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

Issue Type: Bug Created: 2008-12-01T07:12:41.000+0000 Last Updated: 2011-09-14T13:46:46.000+0000 Status: Resolved Fix version(s): Reporter: Piotr Czachur (zimnyx) Assignee: Matthew Weier O'Phinney (matthew) Tags: - Zend_Form

Related issues: Attachments:


Following code:

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



    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".


Posted by Jan Sorgalla (jsorgalla) on 2009-01-07T00:10:42.000+0000

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.

Posted by Piotr Czachur (zimnyx) on 2009-04-10T04:06:33.000+0000

Any plans about this issue?

Posted by Marius Mühlberger (tracer) on 2009-05-25T16:18:21.000+0000

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.

Posted by Mike Willbanks (digitalstruct) on 2009-06-24T06:29:50.000+0000

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.

Posted by Matthew Weier O'Phinney (matthew) on 2009-10-15T09:19:33.000+0000

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.

Posted by Jan Sorgalla (jsorgalla) on 2009-10-15T11:34:45.000+0000

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

And that is exactly what is not working.

<pre class="highlight">
$form->addElement('checkbox', 'confirm', array('required' => true));

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

<pre class="highlight">
array('uncheckedValue' => "")

option to all my checkbox elements.

Posted by Gustavo Straube (gutostraube) on 2010-07-19T11:06:47.000+0000

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

There is some workaround to turn a checbox required?

Posted by Weber Chris (chrisweb) on 2011-09-14T13:46:46.000+0000

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

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.