symfony2 EWZRecaptchaBundle额外字段

时间:2013-01-10 16:57:46

标签: symfony recaptcha

我遇到了EWZRecaptcha Bunlde(dev-master)和symfony 2.1.0的问题。 reCaptcha显示正确,图像更改,所以我认为配置正常。但reCaptcha未经过验证,提交后$form->getErrorsAsString()说:此表单不应包含额外字段。

好吧,我认为从reCaptcha发送的额外字段是recaptcha_challenge_fieldrecaptcha_response_field,但我不认为我错过了the docu中的某些内容,那么它们会出现什么问题?< / p>

对于验证,我使用the docu中的代码:(我也尝试过那里提到的替代方案)

use EWZ\Bundle\RecaptchaBundle\Validator\Constraints as Recaptcha;
//...
/**
* @Recaptcha\True
*/
public $recaptcha;
//...
配置中的

framework:
    validation: { enable_annotations: true }

我添加了这样的字段:

$builder->add('recaptcha', 'ewz_recaptcha', array(
                'property_path' => false,
                'attr' => array(
                    'options' => array(
                        'theme' => 'clean'
                    )
                )
));

也许我忘记了一些必不可少的东西,这在纪录片中没有提到过?

2 个答案:

答案 0 :(得分:0)

可能尝试向构建器添加“约束”选项。我的recaptcha builder add看起来像这样:

    $builder->add('recaptcha', 'ewz_recaptcha', array(
                        'attr'          => array(
                            'options' => array(
                                'theme' => 'red'
                            )
                        ),
                        'label' => "Verification",
                        'property_path' => false,
                        'constraints'   => array(
                            new True()
                        ),
                        'help' => "Enter the words in the box for verification purposes."
                    ));

因此为约束添加'use'语句:

use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\True;

然后添加约束选项:

'constraints'   => array(
    new True()
),

答案 1 :(得分:0)

终于找到了解决方案!
摆脱extra fields我在表单类中添加了这两个字段:

$builder->add('recaptcha_challenge_field', 'hidden', array('property_path' => false));
$builder->add('recaptcha_response_field', 'hidden', array('property_path' => false));

验证然后适用于:

use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\True;
...
'constraints'   => array(
                    new True()
                )

注释不适合为我工作:

use EWZ\Bundle\RecaptchaBundle\Validator\Constraints AS Recaptcha;
...
/**
 * @Recaptcha\True
 */
public $recaptcha;