好吧,你可以用一个新的来替换验证器链。假设我有一个带有两个验证器的元素:
- Zend\Validator\NotEmpty
- Zend\Validator\EmailAddress
我想从中删除 EmailAddress 验证器。你可以这样做:
// 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);
简单的 ;)