【问题标题】:Symfony2 EWZRecaptchaBundle never validate recaptcha fieldSymfony2 EWZRecaptchaBundle 从不验证 recaptcha 字段
【发布时间】:2015-07-08 10:49:46
【问题描述】:

我正在使用 Symfony 2.7,并尝试在我的注册表单中使用 EWZRecaptchaBundle (dev-master)。我已按照documentation 的步骤操作,但它不起作用,永远不要验证recaptcha 字段。

我已经配置了捆绑包:

ewz_recaptcha:
    public_key:  my_public_key
    private_key: my_private_key
    locale_key:  %kernel.default_locale%

我已经在我的班级注册中添加了recaptcha:

// ...
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints as Recaptcha;

class Register
{
    // ...

    /**
     * @Recaptcha\IsTrue
     */
    public $recaptcha;

    // ...
}

在我的 RegisterType 中:

// ...
class RegisterType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // ...  
        ->add('recaptcha', 'ewz_recaptcha');
    }

    public function getName()
    {
        return 'register';
    }
}

在我的表单模板中:

{# ... #}
{{ form_widget(form.recaptcha) }}
{# ... #}

但是当我提交表单时,从不验证 recaptcha 字段并且从不显示错误消息“此值不是有效的验证码”。虽然我不检查。是否有必要在控制器中做一些事情来获取表单提交?

谢谢。

【问题讨论】:

  • 嗯,您可能应该知道使用 I am not a bot 复选框的新 Recaptcha 版本。可能EWZRecaptchaBundle还不支持?
  • 是的,EWZRecaptchaBundle 支持新的 recaptcha 版本。我认为问题在于 Symfony2 验证。
  • 您找到解决方案了吗?遇到同样的问题

标签: validation symfony bundle recaptcha


【解决方案1】:

找到了解决办法。关键是不要使用AJAX。如下设置config.yml和RegisterType:

在 app/config.yml 你应该设置以下内容:

ewz_recaptcha:
public_key: public_key_here
private_key: private_key_here
locale_key: '%kernel.default_locale%'
enabled: true
ajax: false

在 RegisterType 你应该有:

 ->add('recaptcha', EWZRecaptchaType::class,
            [
                'label'       => 'Captcha check:',
                'mapped'      => false,
                'constraints' => [
                    new Recaptcha()
                ],
                'attr'        => [
                    'options' => [
                        'type'  => 'image',
                        'defer' => false,
                        'async' => false,
                        'size'  => 2
                    ]
                ]
            ])

【讨论】:

    【解决方案2】:

    我建议使用gregwar captcha bundle,它是一个强大且易于使用的bundle https://github.com/Gregwar/CaptchaBundle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多