Home » HTML » HTML_Form » Bug #2645
Not able to have a '0' (zero) as default selected value in a select field
Details
| Submitted | 2004-10-29 11:49 UTC |
|---|---|
| From | ipluta at wp dot pl |
| Assigned | danielc |
| Status | Closed |
| Package | HTML_Form |
| PHP Version | 4.3.8 |
| OS | FreeBSD 4.10 |
| Roadmaps | (Not assigned) |
Comments
[2004-10-29 11:49 UTC] ipluta at wp dot pl
Description:
------------
Is is not possible to have '0' (zero) default selected value in a select field of a form. The $default parameter of HTML_Form::returnSelect() is tested with simple
if($default) test, and as a result the '0' value returns false and the method assumes there was no default value specified.
As a temporary fix I use the following wrapper class:
class myHTML_Form extends HTML_Form
{
function returnSelect($name, $entries, $default = '', $size = 1,
$blank = '', $multiple = false, $attr = '')
{
if ($default === '0') {
$default = '__0__'; // make '0' something true to cheat if($default) test inside parent::returnSelect()
}
$str = parent::returnSelect($name, $entries, $default, $size, $blank, $multiple, $attr);
return str_replace('__0__', '0', $str); // replace back with the original '0'
}
}
Reproduce code:
---------------
require "HTML/Form.html";
$select_array = array('0' => 'zero', '1' => 'one');
$html_form =& new HTML_Form('myaction');
print $html_form->returnSelect(
'select_of_html_form',
$select_array,
'0' // this is the value intended
// to be default selected
);
Expected result:
----------------
<select name="select_of_html_form" size="1" >
<option selected="selected" value="0">zero</option>
<option value="1">one</option>
</select>
Actual result:
--------------
<select name="select_of_html_form" size="1" >
<option value="0">zero</option>
<option value="1">one</option>
</select>