How to Force a User to Check a Single Checkbox

Basically you just need to trigger a validation error. I do this by making the unchecked value equal to an empty string and adding the NotEmpty validator.


$this->addElement('checkbox', 'agreement', array(
'label' => 'I agree with all the statements.',
'filters' => array(
'StringTrim'
),
'validators' => array(
array('NotEmpty', true, array('messages' => 'Must check.')),
),
'uncheckedValue' => '', /* This causes a blank value when not check which causes NotEmpty validation to trigger. */
'required' => true
));

Leave a Reply

Your email address will not be published. Required fields are marked *