Zend Framework: how to set a custom validator in a form element?

I am writing a special validator that checks that at least one field matters. I want to confirm that the value "field name" or "idfield" has a value. One of them may be empty or both may be relevant, but at least one of them must be relevant.

$nameField = new Zend_Form_Element_Hidden('namefield');
$nameField->setValue($this->nameFieldValue)
          ->addValidator('AtLeastOneHasValue', false, array('idfield'));

From what I understand, my validator will not check if I have not specified my form element.

->setRequired(true)

But if I install it, it automatically checks that it is not empty, and the error message says that it is empty. I want the field to be empty and check multiple fields with my custom validator. How to check a form element with my custom validator without requiring a form element?

+3
source share
1 answer

Check this documentation page for the setAllowEmpty () method. This should help you get where you are trying to go.

setAllowEmpty ($ flag) and getAllowEmpty () allows you to change the behavior of additional elements (that is, elements where the flag is required - false). When the 'allow empty' flag is true, null values ​​will not be passed to the validator chain.

+6
source

All Articles