PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » HTML » HTML_Form » Bug #2645

Not able to have a '0' (zero) as default selected value in a select field

Details

Submitted2004-10-29 11:49 UTC
Fromipluta at wp dot pl
Assigneddanielc
StatusClosed
PackageHTML_Form
PHP Version4.3.8
OSFreeBSD 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>