Fail

Nov
16th
09

Requiring a checkbox to be ticked in ZendFramework

Zend Framework “documentation” dosn’t say much about validating single checkboxes. If you need to do this, say for a terms ang conditions agreement or something, you have to check that the value is greaterthan zero.

To do this, add a validator like this:

 $e =new Zend_Form_Element_Checkbox('tnc');
$e->addValidator('GreaterThan', false, array(0));

And validate your form in the controller as usual

 if ($form->isValid($request->getPost())) {

Thanks to Rob Knight for adding how to do this in the bug tracker. http://framework.zend.com/issues/browse/ZF-5920