在 InputFilter 中我有这段代码:
$password = new Input('password');
$password->setRequired(false)
->getValidatorChain()
->attach(new Validator\StringLength(6));
$password->getFilterChain()
->attach($this->passwordHash);
问题是过滤器在验证之前应用到值,所以验证器总是返回 true。
我想知道是否有任何方法可以在验证后进行过滤。
最佳答案
Zend\InputFilter\FileInput
类是执行 validation before filtering 的输入类型之一。 .您可以在源代码中看到他们如何解决它,并根据该解决方案创建您自己的 PasswordInput
类。
如你所见in the source code on line 64只要 isValid
为 false
(初始值),就有一个 if 子句阻止过滤器运行。因此验证器首先运行,然后当父级 InputFilter
在验证后再次调用 getValue
以从 FileInput
获取值时运行过滤器。
关于zend-framework2 - Zend Framework 2,在 InputFilter 中验证后过滤值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23113446/