ZF-5122: Required flag set to true doesn't work for checkbox element.

Description

Following code:

    $form = new Zend_Form;
    $form->setMethod('post');
    $form->addElement('checkbox', 'identityVerified', array('required' => true, 'value' => 'confirmed'));
    var_dump($form->identityVerified->getValue());

var_dump($form->isValid(array()));

prints

    string '0' (length=1)

boolean true

but isValid() should be false when no value was passed an required flag=true, like docs suggests: "By default, if an element is required, but does not contain a 'NotEmpty' validator, isValid() will add one to the top of the stack, with the breakChainOnFailure flag set. This makes the required flag have semantic meaning: if no value is passed, we immediately invalidate the submission and notify the user, and prevent other validators from running on what we already know is invalid data."

I understand that checkbox element evaluates to 0 when it's not checked, an that's why required=true doesn't trigger validation error. If so, what's the point of using "required" param for checkbox element? Maybe using "required" flag for this form element should throw an excepion "No such flag".

Comments

I also discovered this bug. This is due to Zend_Validate_NotEmpty changed its handling for strings in 1.5.3. Since it does not use empty() any more on strings, the default $_uncheckedValue ("0") validates to true.

Any plans about this issue?

Because '0' is the default value which is passed, that means that the user did not enter anything. So if this element is required, isValid() should return false in my opinion, instead of throwing an exception as suggested above.

I don't believe this to be a bug so to speak. If you are looking for it to be checked (which required would ensure there is a value) you should use an InArray validator or even checking if the value is equal to something. Changing this may break a lot of production code.

I agree with Mike Willbanks here. Marking a checkbox as "required" indicates that you want it checked. If that's not the case, and you want to simply make sure that the value is valid, then you should use an InArray validator.

{quote} Marking a checkbox as "required" indicates that you want it checked. {quote}

And that is exactly what is not working.


$form->addElement('checkbox', 'confirm', array('required' => true));
var_dump($form->isValid(array()));

That always returns true, also if the checkbox is not checked because the $_uncheckedValue is a string "0" by default. The changed bevahiour in 1.5.3 for Zend_Validate_NotEmpty returns true in this case.

That broke my application after upgrading ZF to 1.5.3 and i had to add a


array('uncheckedValue' => "")

option to all my checkbox elements.

I'm using ZF 1.10 and this issue continues to be unfixed.

There is some workaround to turn a checbox required?

You can use $myFormElement->setUncheckedValue(''); then setRequired will throw an error if box is unchecked, i use this for our Terms and Conditions checkbox.