Developed with love by KnpLabs Hire us for your project!
19

GoogleRecaptchaBundle

by nietonfir

GoogleReCaptchaBundle

ReCAPTCHA is a free CAPTCHA service that protects websites from spam and abuse.
This bundle uses the GoogleReCaptcha library or validating a users "No CAPTCHA reCAPTCHA" response and provides a custom form type, a custom validation constraint as well as a validator to use with the Symfony Form Component.

Latest Stable Version Latest Unstable Version License

Installation

The recommended way to install GoogleReCaptchaBundle is through
Composer.

# Install Composer
curl -sS https://getcomposer.org/installer | php

Next, run the Composer command to install the latest stable version of GoogleReCaptcha:

composer require "nietonfir/google-recaptcha-bundle"

Or add GoogleReCaptchaBundle in your composer.json

"require": {
    "nietonfir/google-recaptcha-bundle": "v0.0.3"
}

and tell Composer to install the library:

composer update "nietonfir/google-recaptcha-bundle"

After installing, don't forget to enable the bundle:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Nietonfir\Google\ReCaptchaBundle\NietonfirGoogleReCaptchaBundle(),
    );
}

Configuration

Add the following simple configuration to your config.yml.
yaml
nietonfir_google_recaptcha:
sitekey: <your_site_key_here>
secret: <and_your_secret_here>
validation: '<your_form_name>'

Multiple forms

ReCaptcha can also be added to different forms (while not on the same page!):
yaml
nietonfir_google_recaptcha:
validation: [ '<your_form_name_A>', '<your_form_name_B>' ]

Custom form field name

The form field name containing the recaptcha response, which defaults to recaptcha, can be customized as well:
yaml
nietonfir_google_recaptcha:
validation:
forms:
- {form_name: '<your_form_name_A>', field_name: 'recaptcha'}
- {form_name: '<your_form_name_B>', field_name: 'recaptcha'}

Additionally you have to add the corresponding form field themes depending on
your used templating engine in config.yml.

# Twig
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    form_themes:
        - 'NietonfirGoogleReCaptchaBundle:Form:fields.html.twig'

# PHP
framework:
    templating:
        form:
            resources:
                - 'NietonfirGoogleReCaptchaBundle:Form'

Usage

Using the Bundle is dead simple:

  1. Create your form type as usual
  2. Add a field using the recaptcha field type

    use Nietonfir\Google\ReCaptchaBundle\Form\Type\ReCaptchaType;
    
    $builder->add('recaptcha', ReCaptchaType::class);
    
  3. Add the necessary javascript library to your template
    html
    <script src='https://www.google.com/recaptcha/api.js' async defer></script>

  4. Make your controller implement ReCaptchaValidationInterface

    use Nietonfir\Google\ReCaptchaBundle\Controller\ReCaptchaValidationInterface;
    
    class DefaultController extends Controller implements ReCaptchaValidationInterface
    

Now when form->isValid() is called, the submitted reCAPTCHA response is validated against the Google API.
Be advised that both the form and the field name used have to be set in config.yml.

TODOs

  • [x] Add some info() to the form & field name config values in Configuration.php
  • [x] Translate the error messages returned from the Google API to something more meaningful
  • [ ] Update documentation
  • [ ] Add some more examples
  • [x] Add missing unit tests
The MIT License (MIT)

Copyright (c) 2014 nietonfir

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

nietonfir_google_recaptcha:

# The sitekey provided by reCAPTCHA.
sitekey: ~ # Required

# The secret provided by reCAPTCHA.
secret: ~ # Required
validation: # Required

# The name of the form that should have a reCAPTCHA.
form_name: ~

# The field name that will hold the reCAPTCHA.
field_name: recaptcha
forms: # Required

# The name of the form that should have a reCAPTCHA.
form_name: ~ # Required

# The field name that will hold the reCAPTCHA.
field_name: recaptcha
  • Version bump
    By nietonfir, 1 year ago
  • Refactor multiple form support
    By nietonfir, 1 year ago
  • Fix configuration - don't require field_name since it has a default value
    By nietonfir, 2 years ago
  • Simplify the configuration if only one form is used
    By nietonfir, 2 years ago
  • Add multiple form support
    By nietonfir, 2 years ago
  • Translate the error codes returned from upstream to something more meaningful
    By nietonfir, 2 years ago
  • Update usage in REAMDE.md
    By nietonfir, 2 years ago
  • Add missing return statement
    By nietonfir, 2 years ago
  • Bump constraints for SF3 support
    By nietonfir, 2 years ago
  • Update documentation
    By nietonfir, 2 years ago
  • Update custom validation constraint
    By nietonfir, 2 years ago
  • Update ReCaptchaType for Symfony3
    By nietonfir, 2 years ago
  • Update ReCaptchaResponseValidator - improve $value checks
    By nietonfir, 2 years ago
  • Remove obsolete NotBlank constraint in ReCaptchaType
    By nietonfir, 2 years ago
  • Don't disable ssl by default when initializing the guzzle http client
    By nietonfir, 2 years ago
  • Drop the 2.4 validation API comment
    By nietonfir, 2 years ago
  • Remve var_dump statement
    By nietonfir, 2 years ago
  • Add some info() to the configuration parameters
    By nietonfir, 2 years ago
  • Use checkboxes in the TODOs list
    By nietonfir, 2 years ago
  • Add a default value to the validation field name
    By nietonfir, 2 years ago
  • Add NietonfirGoogleReCaptchaExtensionTest
    By nietonfir, 2 years ago
  • Add missing phpunit configuration file
    By nietonfir, 2 years ago
  • Fix autoload-dev path - use the same namespace
    By nietonfir, 2 years ago
  • Remove boilerplate files
    By nietonfir, 2 years ago
  • Use @stable for nietonfir/google-recaptcha
    By nietonfir, 2 years ago
  • Merge pull request #3 from OskarStark/patch-1
    By web-flow, 2 years ago
  • added syntax highlighting to README
    By web-flow, 2 years ago
  • Update dependencies in composer.json
    By nietonfir, 4 years ago
  • Update dev-master branch-alias
    By nietonfir, 4 years ago
  • Add some badges
    By nietonfir, 4 years ago