Create a new validator, and apply it to each of the elements. Inside the validator, you also have access to other elements and their values.
Check out the validation documentation as it should explain how to do the custom validators.
Cheers
Hi all,
I have 4 different sets of Zend_Form_Element_MultiCheckbox that I can't combine into one. My problem is that I want to validate these 4 different sets as one and say 'At least one of the checkboxes must be selected'.
I know I need to create a custom validator but could someone give me a pointer on how I then apply this to the 4 separate Zend_Form_Element_MultiCheckbox instances as one?
Cheers,
Tom
Create a new validator, and apply it to each of the elements. Inside the validator, you also have access to other elements and their values.
Check out the validation documentation as it should explain how to do the custom validators.
Cheers
Thanks for the input Adrian.
Unfortunately it appears even if I apply a custom validator to each of the sets of checkboxes, if no checkboxes are selected then none of the validators get called.
My current solution would be to apply a validator to a random element (the submit button for example) and check the state of the checkboxes from there. But surely there's a better way?
This is the code currently by the way...
Code:class BBC_Buzzpage_ReportForm extends Zend_Form { public function __construct() { $offensive = new Zend_Form_Element_MultiCheckbox('o'); $offensive->setLabel("O...") ->addMultiOptions(array( '0' => 'P', '1' => 'H', '2' => 'G' )); $unlawful = new Zend_Form_Element_MultiCheckbox('u'); $unlawful->setLabel("U...") ->addMultiOptions(array( '0' => 'C', '1' => 'B', '2' => 'D', '3' => 'H', '4' => 'D' )); $accessSafety = new Zend_Form_Element_MultiCheckbox('a'); $accessSafety->setLabel("P...") ->addMultiOptions(array( '0' => 'P', '1' => 'S', '2' => 'S', '3' => '1', '4' => 'F', '5' => 'S' )); $promotional = new Zend_Form_Element_MultiCheckbox('p'); $promotional->setLabel("Pr") ->addMultiOptions(array( '0' => 'S', '1' => 'P', '2' => 'C', '3' => 'C' )); $value = new Zend_Form_Element_MultiCheckbox('v'); $value->setLabel("L...") ->addMultiOptions(array( '0' => 'S' )); $reason = new Zend_Form_Element_Text('r'); $reason->setLabel("Use this box to give us more details on your reason:"); $submit = new Zend_Form_Element_Submit('Submit'); $this->addElements(array( $o, $u, $a, $p, $v, $r )); $this->setMethod("post"); $this->clearDecorators(); $this->setDecorators(array(array('ViewScript', array( 'viewScript' => 'path/to/form/reportform.phtml', 'class' => 'form element' )), 'Form')); } }
Last edited by ro88o; 01-29-2010 at 03:06 PM.
Try $element->setRequired(true) - that will force it to run all the validation chians for it, and may also add notEmpty validator, which I think also is what you need?
$element->setAllowEmpty() is probably what you need.
Validate Empty when other field is true
Sorry for the slow reply guys I had to work on something else for a few days, thanks alot for your help.
setAllowEmpty(true) has forced my custom validator to run on the checkboxes as required, however I expected to be able to validate the multicheckbox element as a whole rather than each individual checkbox.
How do I get access to the other form elements whilst in the validator for a checkbox?
I echoed $this while in the validator and it just returned:
Code:object(App_Validate_OOC)[84] protected '_value' => string 'accepted' (length=8) protected '_messageVariables' => array empty protected '_messageTemplates' => array empty protected '_messages' => array empty protected '_obscureValue' => boolean false protected '_errors' => array empty protected '_translator' => null protected '_translatorDisabled' => boolean false public 'zfBreakChainOnFailure' => boolean false
AFAIK you don't have access to the elements. You have access to the values (array of form data) if you define the validator's method asHow do I get access to the other form elements whilst in the validator for a checkbox?
instead of the usualCode:public function isValid($value, $formData) {...}
You can get access to the elements if you pass them to your validator somehow. How to do that is up to you I guess.Code:public function isValid($value) {...}
Last edited by maciek.231; 02-05-2010 at 09:38 AM.