ZF-5325: Zend_Form_Element_Checkbox::isValid() should use isChecked() if required
Description
Checking for required on Zend_Form_Element_Checkbox using notEmpty does not make any sense, in my opinion. "Required" for a checkbox should be defined as the fact that the checkbox was checked; Not that the value is not empty. Here is the solution I am currently using in an extended class of my own:
class ZendExt_Form_Element_Checkbox extends Zend_Form_Element_Checkbox
{
//...
public function isValid($value, $context = null)
{
$this->setValue($value);
if ($this->isRequired() && !$this->isChecked())
{
$translator = $this->getTranslator();
$this->addError($translator->translate('checkboxRequired'));
return false;
}
return parent::isValid($value, $context);
}
//...
}
Comments
Posted by Mike Willbanks (digitalstruct) on 2009-06-24T22:23:31.000+0000
Please see issue: ZF-5399 for a code example of how to utilize the functionality by setting the default uncheckedValue to null.
Posted by Matthew Weier O'Phinney (matthew) on 2009-10-15T09:13:51.000+0000
Please see Mike's comment above. I'd argue that you wouldn't mark the checkbox as "required", and if you do, you would add a validator for ensuring that the appropriate value is checked.