我希望我的联系表格symfony有一个recaptcha系统。为此,我使用了EWZRecaptchaBundle。但是我尝试了很多运行recaptcha的技巧,但我的表单提交没有测试验证recaptcha小部件(看不见和可见)
你能帮我正确地运行recaptcha吗?我的提交表单在没有处理recaptcha的情况下运行,但我正确显示了小部件。
config.yml
ewz_recaptcha:
public_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
locale_key: %kernel.default_locale%
locale_from_request: false
enabled: true
verify_host: true
ajax: false
ContactType
->add('recaptcha', EWZRecaptchaType::class, array(
'attr' => array(
'options' => array(
'theme' => 'light',
'type' => 'image',
'size' => 'invisible',
'defer' => true,
'async' => true,
'callback' => 'onReCaptchaSuccess',
'bind' => 'contact_submit',
)
),
'mapped' => false,
'constraints' => array(
new RecaptchaTrue()
)
)
)
// ->add('recaptcha', EWZRecaptchaType::class)
->add('submit', SubmitType::class, [
'label' => 'form.submit.send',
'attr' => ['class' => 'btn1 form_recaptcha_submit', 'id' => 'contact_submit']
])
Twig模板
{{ form_widget(form.recaptcha, { 'attr': {
'options' : {
'theme': 'light',
'type': 'image',
'size': 'invisible'
},
} }) }}
联系(实体)
我有我的字段recaptcha
private $recaptcha;
答案 0 :(得分:0)
您需要在表单中定义验证(也可以在实体类中进行验证)。同样在我的情况下,我没有在我的能量中创造一个领域。我更喜欢使用我们称之为“非映射”的字段。要使用非映射字段,您只需使用'mapped'=> false,在你的contactType
中我在下面显示了我使用的代码:
我的联系人类型:
->add('recaptcha', EWZRecaptchaType::class, array(
'attr' => array(
'options' => array(
'theme' => 'light',
'type' => 'image',
'size' => 'normal',
'defer' => true,
'async' => true,
)
)
,
'mapped' => false,
'constraints' => array(
new RecaptchaTrue()
)
))
我给你我的config.yml所以你可以看到我不需要定义那么多的选项来使recaptcha工作:
ewz_recaptcha:
public_key: xxxx
private_key: xxxx
# Not needed as "%kernel.default_locale%" is the default value for the locale key
locale_key: %kernel.default_locale%
locale_from_request: true
编辑:在我的模板中显示带有twig的recaptcha小部件,我只使用{{form_end(form)}},并且recaptcha小部件位于提交按钮的顶部。
编辑2:你不需要在你的控制器中进行任何验证,但是这个:
if ($contactForm->isSubmitted() && $contactForm->isValid())
{
//your logic
}