4
votes

I read this question on SO: "how to disable inArray validator forms in zend framework2" and was trying to find it out, but couldn't find any way to detach/remove the InArray Validator. But InArray is just a validator. So how can remove a validator from the validator list of a form element?

I can get the validators:

$myElement = $form->getInputFilter()->get('city');
$validatorChain = $cityElement->getValidatorChain();
$validators = $validatorChain->getValidators();

and maybe can then unset the array element with the validator, I want to remove, and then pass the result array back to the Input object and to the form element. But it's really dirty and surely not the recommended way.

So how to remove a validator from a form element?

2
did you try $inputFilter = new InputFilter(); $inputFilter->remove($name); And do you use $factory = new InputFactory(); for example? - Remi Thomas
Thank you! Yes, it works: $formInputFilter = $form->getInputFilter(); $formInputFilter->remove('city'); $formInputFilter->add((new Zend\InputFilter\Factory())->createInput(array( 'name' => 'city', 'required' => true, ))); But then I would have to create an Input in a view script. Dirty. - automatix
In your view just do $newInputFilter = new InputFilter(); //.. your stuff.. $inputFilter = new InputFilter(); $inputFilter->add($newInputFilter, "yourElement"); $form->setInputFilter($inputFilter); But it will be better in your controller! - Remi Thomas
Yes, the only clean(-er) way seems to extend the InputFilter, and/or Input class, and maybe also the Element. - automatix

2 Answers

6
votes

Well, you could just replace the validator chain with a new one. Let's say I have an element with two validators:

  • Zend\Validator\NotEmpty
  • Zend\Validator\EmailAddress

And I want to remove the EmailAddress validator from it. You could do something like this:

// create new validator chain
$newValidatorChain = new \Zend\Validator\ValidatorChain;
// loop through all validators of the validator chained currently attached to the element
foreach ($form->getInputFilter()->get('myElement')->getValidatorChain()->getValidators() as $validator) {
    // attach validator unless it's instance of Zend\Validator\EmailAddress
    if (!($validator['instance'] instanceof \Zend\Validator\EmailAddress)) {
        $newValidatorChain->addValidator($validator['instance'], $validator['breakChainOnFailure']);
    }
}
// replace the old validator chain on the element
$form->getInputFilter()->get('myElement')->setValidatorChain($newValidatorChain);

Easy ;)

3
votes

I found this to work with 1.12.3

in my update form

$element = new My_Form_Element_Username('username');
$element->setValue('some-value');
$element->removeValidator('Db_NoRecordExists');
$this->addElement($element);

or

$this->addElement(new My_Form_Element_Username('username')
  ->setValue('some-value')
  ->removeValidator('Db_NoRecordExists');

My_Form_Element_Username just extends some Zend_Form_Element and has the validators defined.