MaelRecaptchaBundle is a bundle allowing the integration of Google Recaptcha on a Symfony project.
- Google ReCaptcha V2 (invisible) : ✅
- Google ReCaptcha V2 (checkbox) : ✅
- Google ReCaptcha V3 : ❌
It's very quick and easy, in 5 steps
- Install MaelRecaptchaBundle via composer
- Enable the bundle
- Generate you key and secret key
- Configure your key and secret key
- Use the Recaptcha in your forms
- Contributing
- License
Run the following command :
composer require mael/recaptcha-bundle
You can quickly configure this bundle by using symfony/flex
:
- Answer no for
google/recpatcha
- Answer yes for
mael/recaptcha-bundle
Register bundle into config/bundles.php
<?php
return [
Mael\MaelRecaptchaBundle\MaelRecaptchaBundle::class => ['all' => true],
];
Go to the following link : http://www.google.com/recaptcha/admin
In you .env
file
Replace YOUR_RECAPTCHA_KEY
by your public key and YOUR_RECAPTCHA_SECRET
by your private key
MAEL_RECAPTCHA_KEY=YOUR_RECAPTCHA_KEY
MAEL_RECAPTCHA_SECRET=YOUR_RECAPTCHA_SECRET
To enable Recaptcha (invisible) protection on your form, you must use this type: MaelRecaptchaSubmitType::class
// For example
->add('captcha', MaelRecaptchaSubmitType::class, [
'label' => 'Submit',
'constraints' => new MaelRecaptcha()
])
For the third parameter which is an array, you can add the constraint : MaelRecaptcha
Then, to complete the configuration of the invisible Recaptcha, in your twig file you need to add a id to your form
{{ form_start(your_form, {'attr': {'id': 'form-recaptcha'}}) }}
To finish, add 2 <script>
tags
Replace "id-of-your-form" by the class of your form add just above it
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmitCaptcha(token) {
document.getElementById("class-of-your-form").submit();
}
</script>
Warning, you cannot change the name of the JavaScript function.
To enable Recaptcha (checkbox) protection on your form, you must use this type: MaelRecaptchaCheckboxType::class
->add('captcha_checkvox', MaelRecaptchaCheckboxType::class, [
'constraints' => new MaelRecaptcha()
])
For the third parameter which is an array, you can add the constraint : MaelRecaptcha
To finish, in your twig file add <script>
tag
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
List of contribution HERE
You want contribute ? Fork this repertory and create a pull request after change
You can find the license in the root directory