Symfony2将reCaptcha字段添加到注册表单 [英] Symfony2 add reCaptcha field to registration form

查看:121
本文介绍了Symfony2将reCaptcha字段添加到注册表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将EWZRecaptcha添加到我的注册表中. 我的注册表单生成器如下所示:

I'm trying to add EWZRecaptcha to my registration form. My registration form builder looks something like this:

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('username',  'text')
            ->add('password')
            ->add('recaptcha', 'ewz_recaptcha', array('property_path' => false));
}

public function getDefaultOptions(array $options)
{
    return array(
            'data_class' => 'Acme\MyBundle\Entity\User',
    );
}

现在,如何将Recaptcha约束添加到验证码字段中?我试图将其添加到validation.yml:

Now, how can I add the Recaptcha Constraint to the captcha field? I tried to add this to validation.yml:

namespaces:
  RecaptchaBundle: EWZ\Bundle\RecaptchaBundle\Validator\Constraints\

Acme\MyBundle\Entity\User:
  ...
  recaptcha:
    - "RecaptchaBundle:True": ~

但是我收到Property recaptcha does not exists in class Acme\MyBundle\Entity\User错误.

如果我从recaptcha字段的选项中删除array('property_path' => false),则会收到错误消息:

If I remove array('property_path' => false) from the recaptcha field's options, I get the error:

Neither property "recaptcha" nor method "getRecaptcha()" nor method "isRecaptcha()"
exists in class "Acme\MyBundle\Entity\User"

任何想法如何解决? :)

Any idea how to solve it? :)

推荐答案

Acme\MyBundle\Entity\User没有recaptcha属性,因此在尝试验证User实体上的该属性时会收到错误消息.设置'property_path' => false是正确的,因为这告诉Form对象它不应尝试获取/设置域对象的此属性.

Acme\MyBundle\Entity\User does not have a recaptcha property, so you're receiving errors for trying to validate that property on the User entity. Setting 'property_path' => false is correct, as this tells the Form object that it shouldn't try to get/set this property for the domain object.

那么您如何才能在此表单上验证该字段并仍然保留您的User实体?很简单-甚至在文档中进行了说明.您需要自己设置约束并将其传递给FormBuilder.这是您应该得到的:

So how can you validate that field on this form and still persist your User entity? Simple - it is even explained in the documentation. You'll need to setup the constraint yourself and pass it to the FormBuilder. Here is what you should end up with:

<?php

use Symfony\Component\Validator\Constraints\Collection;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\True as Recaptcha;

...

    public function getDefaultOptions(array $options)
    {
        $collectionConstraint = new Collection(array(
            'recaptcha' => new Recaptcha(),
        ));

        return array(
            'data_class' => 'Acme\MyBundle\Entity\User',
            'validation_constraint' => $collectionConstraint,
        );
    }

我对此方法一无所知,是此约束集合是否将与您的validation.yml合并或是否将其覆盖.

The one thing I don't know about this method, is whether this constraint collection will be merged with your validation.yml or if it will overwrite it.

您应该阅读这篇文章,其中对-深入研究建立表单并验证实体和其他属性的正确过程.它特定于MongoDB,但适用于任何Doctrine实体.紧随本文之后,只需将其termsAccepted字段替换为您的recaptcha字段即可.

You should read this article which explains a bit more in-depth the proper process for setting up forms with validation for entities and other properties. It is specific to MongoDB but applies to any Doctrine entity. Following this article, just replace its termsAccepted field with your recaptcha field.

这篇关于Symfony2将reCaptcha字段添加到注册表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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