Forms Symfony2将reCaptcha字段添加到注册表

Forms Symfony2将reCaptcha字段添加到注册表,forms,symfony,recaptcha,symfony-2.0,Forms,Symfony,Recaptcha,Symfony 2.0,我正在尝试将EWZRecaptcha添加到我的注册表中。 我的注册表单生成器如下所示: public function buildForm(FormBuilder $builder, array $options) { $builder->add('username', 'text') ->add('password') ->add('recaptcha', 'ewz_recaptcha', array('propert

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

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',
    );
}
现在,我如何向captcha字段添加Recaptcha约束?我试图将此添加到validation.yml:

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

Acme\MyBundle\Entity\User:
  ...
  recaptcha:
    - "RecaptchaBundle:True": ~
但是我得到的
属性recaptcha在Acme\MyBundle\Entity\User类中不存在
错误

如果我从recaptcha字段的选项中删除
array('property\u path'=>false)
,我会得到错误:

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

你知道怎么解决吗?:)

Acme\MyBundle\Entity\User
没有
recaptcha
属性,因此您在尝试验证
用户
实体上的属性时收到错误。设置
'property\u path'=>false
是正确的,因为这会告诉
表单
对象它不应该尝试为域对象获取/设置此属性

那么,如何验证此表单上的字段,并仍然保留您的
用户
实体呢?简单-甚至在中有解释。您需要自己设置约束并将其传递给
FormBuilder
。以下是您应该得到的结果:

<?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,
        );
    }

自Symfony 2.1以来,应使用
mapped=false
而不是
property\u path=false
,请分别参阅和。