mael/recaptcha-bundle

Google Recaptcha integration for Symfony and Twig (easy integration with Symfony Form)

Installs: 1 081

Dependents: 0

Suggesters: 1

Security: 0

Stars: 2

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

1.0.0 2020-05-23 19:56 UTC

This package is auto-updated.

Last update: 2024-04-13 00:12:11 UTC


README

68747470733a2f2f7472617669732d63692e636f6d2f4d61656c2d39312f4d61656c52656361707463686142756e646c652e7376673f6272616e63683d6d6173746572 68747470733a2f2f636f6465636f762e696f2f67682f4d61656c2d39312f4d61656c52656361707463686142756e646c652f6272616e63682f6d61737465722f67726170682f62616467652e737667 68747470733a2f2f626574746572636f64656875622e636f6d2f656467652f62616467652f4d61656c2d39312f4d61656c52656361707463686142756e646c653f6272616e63683d6d6173746572

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