zend-framework2 - Zend Framework 2,在 InputFilter 中验证后过滤值

标签 zend-framework2 zend-filter zend-inputfilter

在 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只要 isValidfalse(初始值),就有一个 if 子句阻止过滤器运行。因此验证器首先运行,然后当父级 InputFilter 在验证后再次调用 getValue 以从 FileInput 获取值时运行过滤器。

关于zend-framework2 - Zend Framework 2,在 InputFilter 中验证后过滤值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23113446/

相关文章:

php - zend 框架 2 : How to create action in specific controller in specific action

mysql - 在 Zend 框架 2 的子查询中使用 limit 和 offset

php - Zend Framework 2 - 翻译标准表单验证和错误消息

validation - 如何在 Zend 框架 2 中使用没有表单类的 InputFilter

php - 如何将新的验证器添加到字段到集合(fieldset)中

php - Zend Framework 2 和 SELECT count(*) 查询

zend-framework - 如何过滤/验证 bool 值?

security - 在Zend应用程序中处理无效输入的最安全方法是什么?

phpunit - 如何使用 PHPUnit 测试我的 Zend Framework 2 InputFilter?