ZF-2750: Zend_Form_Element_Checkbox - setValue inconsistent with getValue
Description
When using Zend_Form_Element_Checkbox elements, getValue returns either 1 (checked) or 0 (unchecked). I find that great. However, the current setValue works differently:
public function setValue($value)
{
$value = ($value === null) ? 0 : 1;
$this->checked = ($value === 0) ? false : true;
return parent::setValue($value);
}
Anything other than null, sets the checkbox value to 1 (even 0 or '0'). I would suggest keeping it consistent with getValue, something along these lines:
public function setValue($value)
{
if ($value === null) $value = 0;
$this->checked = ($value == 0) ? false : true;
return parent::setValue($value);
}
I would also suggest allowing the value to be a string ('0' or '1') -> thus a ==. This will allow populating with the data straight from the DB.
Comments
Posted by Matthew Weier O'Phinney (matthew) on 2008-03-05T10:31:35.000+0000
This is one symptom of multiple issues with checkboxes. I will be fixing these for the 1.5.0GA release.
Posted by Matthew Weier O'Phinney (matthew) on 2008-03-07T11:39:28.000+0000
All checkbox issues are corrected in trunk and 1.5 release branch.
Notes: