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

Please see issue: ZF-5399 for a code example of how to utilize the functionality by setting the default uncheckedValue to null.

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.