Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Smarty 2.6.0 - html_options [ Bug + BugFix + Explanation ]

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
putaso
Smarty Rookie


Joined: 10 Oct 2003
Posts: 15

PostPosted: Wed Dec 03, 2003 2:10 am    Post subject: Smarty 2.6.0 - html_options [ Bug + BugFix + Explanation ] Reply with quote

Smarty Version: Smarty 2.6.0

Hi everybody,
I've found a bug in the html_options functions;

It happens when Smarty has to select the default values, and you have a select list and you have and empty value i.e value="", and a cero value, ie. value=0 or value="0",

Here's some example code:

- - - bug.php - - -
[php]
<?php
$smarty = new Smarty_App;

$select = array( 1 => "One", 2 => "Two", "" => "empty", 0 => "Zero", "22" => "Twenty-two");
$smarty->assign('select', $select);
$smarty->display('select.tpl');
?>
[/php]
- - - bug.php - - -

- - - select.tpl - - -
Code:

{* Smarty *}
<form method="post" action="">
   {html_options name="select" options=$select selected=$smarty.post.select}
   <input type="submit">
</form>

- - - select.tpl - - -


The problem rises when you have in the same list value="", and value=0,
since in_array() function will treat them as FALSE,
that is why you need it cast type it or use settype() instead.


Here's an example:
1. You select "ZERO" and you will get:

Code:

<form method="post" action="">
<select name="select">
<option label="One" value="1">One</option>
<option label="Two" value="2">Two</option>
<option label="empty" value="">empty</option>
<option label="Zero" value="0" selected="selected">Zero</option>
<option label="Twenty-two" value="22">Twenty-two</option>
</select>
<input type="submit">
</form>


2. You select "EMPTY" and you'll incorrectly get: (note that both "empty" and "zero" options get "selected='selected'")

Code:

<form method="post" action="">
<select name="select">
<option label="One" value="1">One</option>
<option label="Two" value="2">Two</option>
<option label="empty" value="" selected="selected">empty</option>
<option label="Zero" value="0" selected="selected">Zero</option>
<option label="Twenty-two" value="22">Twenty-two</option>
</select>
<input type="submit">
</form>



BUGFIX:

fucntion.html_options.php

around line #98,
in the function: smarty_function_html_options_optoutput();
you have:
[php]
if (in_array($key, $selected))
[/php]

and you need to change it either to:
[php]
settype($key, "string");
if (in_array($key, $selected))
[/php]

or to:
[php]
if (in_array((string)$key, $selected))
[/php]
--------------

That's it.
Hope it helps you all boys and girls,
since it took a whole while to really understand what was going on and found the solution: ( i knew what I had to do, but for some unknown reason something was not working as excepected)


More information:
http://php.net/in-array
and see the comment on "14-Feb-2002 02:38",

try this:
[php]
<?php
print in_array("", array(0, 'A', 'B')) ? "1. In array\n" : "1. Not in array\n";
print in_array((string)"", array(0, 'A', 'B')) ? "2. In array\n" : "2. Not in array\n";
print in_array("", array(0, 'A', 'B')) ? "3. In array\n" : "3. Not in array\n";
print in_array((string)"", array((string)0, 'A', 'B')) ? "4. In array\n" : "4. Not in array\n";
?>
[/php]
and you'll be getting:
Code:

1. In array
2. In array
3. In array
4. Not in array

so, the only correct example is the last one.

Conclusion: if you are going to use in_array() and you may look for an empty string, always casttype both elements to string, or use settype($element, "string");

putaso


Last edited by putaso on Wed May 05, 2010 10:39 pm; edited 1 time in total
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Dec 09, 2003 7:33 pm    Post subject: Reply with quote

this is a bug. i'll fix it soon.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Tue Dec 09, 2003 7:48 pm    Post subject: Reply with quote

i committed a fix to cvs that doesn't cast the values, but uses "strict" (the optional third parameter of in_array) comparason. my tests look good, but tell me if i overlooked something.

greetings
messju

[EDIT: it seems strict comparason was a little too strict. the elements of $selected and $key are casted to strings now. thanks for finding this bug!]
Back to top
View user's profile Send private message Send e-mail Visit poster's website
putaso
Smarty Rookie


Joined: 10 Oct 2003
Posts: 15

PostPosted: Wed Dec 10, 2003 10:30 am    Post subject: Reply with quote

Hi messju:
yeahh... i've tried strict comparisons before posting this bug, and it didn't work at all (as you said, it was just strict).
I haven't downloaded the latest CVS version to see the changes reflected yeat, but the only two possible solutions I could find were either using settype() or cast typing it to (string),
so if you did the same thing, i guess it might be working alright.

I'm glad i could help!

...and thanks for taking the time to reply,
putaso

________
Box Vaporizer


Last edited by putaso on Sat Mar 12, 2011 12:35 pm; edited 2 times in total
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Dec 10, 2003 10:44 am    Post subject: Reply with quote

i use "(in_array((string)$key, $selected)" as you suggested.
additionally i cast all values of the $selected-array to strings.

thanks for your help
Back to top
View user's profile Send private message Send e-mail Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Dec 10, 2003 11:01 am    Post subject: Reply with quote

If I can make a small suggestion:

How about leveraging PHP's short-circuit evaluation to avoid unnecessary casting?

Something like:

[php:1:6e009ffe06]if (in_array($key, $selected) || in_array((string)$key, $selected)[/php:1:6e009ffe06]

Perhaps even get tricky and cast $selected only as required:

[php:1:6e009ffe06]if (in_array($key, $selected) || in_array((string)$key, array_walk($selected, create_function('&$v,$k','$v = (string)$v;')))[/php:1:6e009ffe06]

Just a thought.

EDIT: On second thought, it probably won't address the issue.
*boots slaps self
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Wed Dec 10, 2003 1:24 pm    Post subject: Reply with quote

Code:
if (in_array($key, $selected) || in_array((string)$key, array_walk($selected, create_function('&$v,$k','$v = (string)$v;')))


Shocked Yet another proof how powerful the PHP interpreter can be ... and how unreadable the code can get Wink
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Wed Dec 10, 2003 5:54 pm    Post subject: Reply with quote

@andre, true, true -- but I was hoping for a real snappy one-liner Smile

Too bad it doesn't do anything useful in this case Wink
Back to top
View user's profile Send private message
WonkyRuler
Smarty n00b


Joined: 19 May 2008
Posts: 1

PostPosted: Mon May 19, 2008 11:55 am    Post subject: Reply with quote

Hi there,

I have come across this bug to but i have followed putaso's instructions and now it is all working fine.

Thanks putaso for leaving those instructions! Smile
_________________
| Label Printing | Printed Labels | - WonkyRuler
Back to top
View user's profile Send private message
huglester
Smarty Rookie


Joined: 09 Oct 2008
Posts: 8

PostPosted: Mon Nov 24, 2008 7:32 am    Post subject: Reply with quote

WonkyRuler wrote:
Hi there,

I have come across this bug to but i have followed putaso's instructions and now it is all working fine.

Thanks putaso for leaving those instructions! Smile



hello.
this one wasn't fixed so far?
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Mon Nov 24, 2008 5:46 pm    Post subject: Reply with quote

huglester wrote:
this one wasn't fixed so far?


my guess is WonkyRuler is just a spam-bot that wanted to say something meaningless and leave his signature-link-spam.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Bugs All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP