|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
putaso Smarty Rookie
Joined: 10 Oct 2003 Posts: 15
|
Posted: Wed Dec 03, 2003 2:10 am Post subject: Smarty 2.6.0 - html_options [ Bug + BugFix + Explanation ] |
|
|
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 |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Dec 09, 2003 7:33 pm Post subject: |
|
|
this is a bug. i'll fix it soon. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue Dec 09, 2003 7:48 pm Post subject: |
|
|
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 |
|
putaso Smarty Rookie
Joined: 10 Oct 2003 Posts: 15
|
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Dec 10, 2003 10:44 am Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Dec 10, 2003 11:01 am Post subject: |
|
|
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 |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Wed Dec 10, 2003 1:24 pm Post subject: |
|
|
Code: | if (in_array($key, $selected) || in_array((string)$key, array_walk($selected, create_function('&$v,$k','$v = (string)$v;'))) |
Yet another proof how powerful the PHP interpreter can be ... and how unreadable the code can get |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed Dec 10, 2003 5:54 pm Post subject: |
|
|
@andre, true, true -- but I was hoping for a real snappy one-liner
Too bad it doesn't do anything useful in this case |
|
Back to top |
|
WonkyRuler Smarty n00b
Joined: 19 May 2008 Posts: 1
|
Posted: Mon May 19, 2008 11:55 am Post subject: |
|
|
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! _________________ | Label Printing | Printed Labels | - WonkyRuler |
|
Back to top |
|
huglester Smarty Rookie
Joined: 09 Oct 2008 Posts: 8
|
Posted: Mon Nov 24, 2008 7:32 am Post subject: |
|
|
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! |
hello.
this one wasn't fixed so far? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Mon Nov 24, 2008 5:46 pm Post subject: |
|
|
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 |
|
|
|
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
|
|