使用输入过滤器时,将忽略ZF2验证码验证 [英] ZF2 Captcha validation ignored when using an input filter

查看:103
本文介绍了使用输入过滤器时,将忽略ZF2验证码验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ZF2应用中有一个带有CAPTCHA元素的表单,如下所示:

I have a form in my ZF2 app with a CAPTCHA element as follows :

$this->add(array(
        'type' => 'Zend\Form\Element\Captcha',
        'name' => 'captcha',
        'attributes' => array(
            'class'=>'form-control',
        ),
        'options' => array(
            'label' => 'Please verify you are human.',
            'captcha' => array('class' => 'Dumb')
        ),
    ));

我在表单上附加了一个输入过滤器,用于验证表单中的其他元素(名称,电子邮件,消息).将其附加到表单后,如果检查验证码是否有效,则会忽略CAPTCHA字段的验证.

I have an input filter attached to the form that validates the other elements in the form (name, email, message). When this is attached to the form the validation for the CAPTCHA field is ignored when checking if valid.

if ($request->isPost()) {          
        // set the filter
        $form->setInputFilter($form->getInputFilter());
        $form->setData($request->getPost());
        if ($form->isValid()) { ...

如果我删除输入过滤器,则验证码字段正确验证,但显然其他字段没有验证程序.我犯了什么愚蠢的错误?我必须在输入过滤器中设置"CAPTCHA"验证器吗?

If i remove the input filter then the CAPTCHA field is validated correctly but obviously the other fields have no validators. What silly mistake am I making? Is there a "CAPTCHA" validator I have to set in the input filter?

推荐答案

问题是因为,我认为您在表单上创建了一个名为:

The issue is because, I assume that on your form you have created a method called:

getInputFilter();

getInputFilter();

它将覆盖原始的getInputFilter(),

which overrides the original getInputFilter(),

有两种解决方案:

将表单上的函数重命名为getInputFilterCustom()

rename your function on your form to be getInputFilterCustom()

然后也进行修改:

if ($request->isPost()) {          
    // set the filter
    $form->setInputFilter($form->getInputFilterCustom());

或在当前的getInputFilter()内部添加验证验证码的逻辑.

or inside your current getInputFilter() add the logic to validate the captcha.

这篇关于使用输入过滤器时,将忽略ZF2验证码验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆