milanpol / recaptcha-bundle
Symfony的Recaptcha包装器
V1.3
2021-06-30 08:04 UTC
Requires
- php: >=7.4
- google/recaptcha: ^1.2
- symfony/form: ^4.4
- symfony/framework-bundle: ^4.4
- symfony/validator: ^4.4
- twig/twig: ^2.0|^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- phpmd/phpmd: ^2.8
- sebastian/phpcpd: ^5.0
- sensiolabs/security-checker: ^6.0
- squizlabs/php_codesniffer: ^3.3
README
使用方法
将任何表单扩展到AbstractRecaptchaType
。这需要您定义getExpectedAction()
。返回预期操作(例如,对于联系表单返回contact
)。
接下来,在包含表单的twig模板中,包括以下内容
{% include '@EsitesRecaptcha/recaptcha/recaptcha.html.twig' %}
最后,使用php bin/console assets:install
安装资源
错误
当recaptcha检查失败时,将向表单添加错误。要显示它,使用
{{ form_errors(form) }}
消息可以通过在validators.nl.yml中定义以下内容进行翻译
error: recaptcha_failed_to_validate: 'Recaptcha failed!'
完整配置
esites_recaptcha: enable_recaptcha: true # default value recaptcha_key: google_recaptcha_key # required, the key from the google recaptcha admin dashboard recaptcha_secret: google_recaptcha_secret # required, the secret key from the google recaptcha admin dashboard recaptcha_score: 0.5 # default value, the minimum score a visitor has to score to not be flagged as a bot expected_hostname: e-sites.nl # not required, the expected hostname in the google recaptcha response use_client_ip: true # default value, also sends the clients ip to verify the recaptcha request
手动更新Recaptcha
选择要更新的recaptcha元素(隐藏的输入),然后调用setRecaptcha();
setRecaptcha(document.querySelector('#recaptchaObject'));