/MaelRecaptchaBundle

Simple twig integration for Google ReCaptcha

Primary LanguagePHPMIT LicenseMIT

MaelRecaptchaBundle for Symfony

MaelRecaptchaBundle is a bundle allowing the integration of Google Recaptcha on a Symfony project.

Available features

  • Google ReCaptcha V2 (invisible) : ✅
  • Google ReCaptcha V2 (checkbox) : ✅
  • Google ReCaptcha V3 : ❌

Installation

It's very quick and easy, in 5 steps

  1. Install MaelRecaptchaBundle via composer
  2. Enable the bundle
  3. Generate you key and secret key
  4. Configure your key and secret key
  5. Use the Recaptcha in your forms
  6. Contributing
  7. License

Step 1 : Install MaelRecaptchaBundle via composer

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

Step 2: Enable the bundle

Register bundle into config/bundles.php

<?php

return [
    Mael\MaelRecaptchaBundle\MaelRecaptchaBundle::class => ['all' => true],
];

Step 3: Generate your key and secret key

Go to the following link : http://www.google.com/recaptcha/admin

Setp 4: Configure your key and secret key

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

Step 5: Editing your form and your view

For Google ReCaptcha V2 (invisible)

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.

For Google ReCaptcha V2 (checkbox)

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>

Contributing

List of contribution HERE

You want contribute ? Fork this repertory and create a pull request after change

License

You can find the license in the root directory