5

Is there a way of detaching a validator from an input? e.g.

$input->getValidatorChain()
    ->attach('email_address')
    ->attach('no_record_exists');

if($isExistingUser == true) {
    $input->getValidatorChain()
        ->remove('no_record_exists');
}
gawpertron
  • 1,867
  • 3
  • 21
  • 37
  • 1
    Do you really need to remove the validator? The form has a `setValidationGroup` method which can be used to ignore validation on specific fields, see for example -> http://stackoverflow.com/questions/15300105/how-can-i-pass-some-data-to-a-validationfilter-zend-2/15313588#15313588 – Crisp Jun 11 '13 at 14:31
  • Yes that is a possible option, but I would still like the input to be validated. In my example I would still want to check that it is a valid email address – gawpertron Jun 11 '13 at 14:37
  • Ok, that's why I asked. It does pose another question though :) I'm guessing you don't want to check for dups when the email hasn't changed, but if it hasn't changed and it was valid to begin with, does it really need to be validated again? :-/ – Crisp Jun 11 '13 at 14:45
  • 2
    To answer your question though, see http://stackoverflow.com/questions/16252520/how-to-remove-a-validator-from-a-form-element-form-element-validatorchain-in-z, http://stackoverflow.com/questions/15782107/zend-framework-2-removed-form-element-causes-validation-to-fail and perhaps http://zend-framework-community.634137.n4.nabble.com/ValidatorChain-remove-and-edit-validators-td4656571.html – Crisp Jun 11 '13 at 14:49
  • The second URL's method sounds like a tidy way of solving it – gawpertron Jun 11 '13 at 14:55

2 Answers2

3
$form->getInputFilter()->remove('no_record_exists');

Form being a Zend\Form\Form object

Adam H
  • 244
  • 2
  • 4
2

To disable e.g. the required-validation, you can do :

$form->getInputFilter()->get('form-field')->setRequired(false);

Pascal Paulis
  • 277
  • 4
  • 17
  • I had to add the entity, like this way: $form->getInputFilter()->get('appointment')->get('street')->setRequired(FALSE); – cwhisperer Nov 28 '16 at 12:37