dmishh/recaptcha-bundle

reCAPTCHA support for Symfony2 with easy-to-use integration to Security Component.

Installs: 12 330

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 4

Forks: 3

Open Issues: 2

Type:symfony-bundle

1.0.1 2014-12-20 09:11 UTC

This package is auto-updated.

Last update: 2024-04-14 06:34:59 UTC


README

Features

  • Easy-to-use
  • Service-oriented architecture
  • Integration into forms
  • Integration into Security Component — for protecting login form with reCAPTCHA
  • Independent reCAPTCHA API realization (Recaptcher by default)
  • Both, PHP and Twig template engines support

Installation using Composer

  1. Add the following to your composer.json file:

    // composer.json
    {
        // ...
        "require": {
            // ...
            "dmishh/recaptcha-bundle": "1.0.*"
        }
    }
  2. Update dependencies, run from command line:

    php composer.phar update
  3. Register the bundle in your AppKernel.php file:

    <?php
    
    // in AppKernel::registerBundles()
    $bundles = array(
        // ...
        new Dmishh\Bundle\RecaptchaBundle\RecaptchaBundle()
    );

Configuration

Your reCAPTCHA's public and private keys that can be found at your recaptcha admin page. Configure RecaptchaBundle in your config.yml:

# ...

recaptcha:
    public_key: 6LeJg90SAAAAAA9yk0zeNrF8QKaxqR_bV_9SNLz9
    private_key: 6LeJg90SAAAAAEuTLEbZuymhkigzzPm2_wsSdA8j
    use_https: false # optional

Usage in forms

Add the following line to create the reCAPTCHA field:

<?php

// your form ...

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', 'recaptcha');
}

You can pass extra options to reCAPTCHA with the widget_options option:

<?php

// your form ...

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', 'recaptcha', array(
        'widget_options' => array(
            'theme' => 'clean'
        )
    ));
}

List of valid options:

  • theme
  • lang
  • custom_translations
  • custom_theme_widget
  • tabindex

Visit Customizing the Look and Feel of reCAPTCHA for the details of customization.

Validation

RecaptchaType has built-in validator, you don't need to do anything more. Just use it!

But if you need to disable validation for some reasons, then remove existing validator:

<?php

// your form ...

public function buildForm(FormBuilder $builder, array $options)
{
    // ...
    $builder->add('recaptcha', 'recaptcha', array(
        // only for disabling validation
        'constraints'   => array()
    ));
}

Usage in login form

You need to define RecaptchaFormAuthenticationListener as default listener for form authentication in services.yml:

# ...

security.authentication.listener.form:
    class: Dmishh\Bundle\RecaptchaBundle\Security\Firewall\RecaptchaFormAuthenticationListener
    parent: security.authentication.listener.abstract
    calls:
        - [setRecaptcha, [@recaptcha]]
    abstract: true

Second parameter in setRecaptcha method is used for disabling validator. This might be useful when you need to disable check in dev environment. For example:

security.authentication.listener.form:
    class: Dmishh\Bundle\RecaptchaBundle\Security\Firewall\RecaptchaFormAuthenticationListener
    parent: security.authentication.listener.abstract
    calls:
        - [setRecaptcha, [@recaptcha, %kernel.debug%]]
    abstract: true

Now you need to add recaptcha field to your form. If you are using your own form type, than add recaptcha field to your form as described [above](#Usage in forms).

Otherwise, add those lines to your Controller which renders login form and to corresponding Twig template:

<?php

// your controller ...

public function yourAction(Request $request) {
    // ...
    $recaptcha = $this->createForm($this->get('form.type.recaptcha'));

    return array(
        // ...
        'recaptcha' => $recaptcha->createView()
    );
}
<!-- template -->
<label for="recaptcha_response_field">Captcha:</label>
{{ form_widget(recaptcha) }}

This documentation is based on EWZRecaptchaBundle's docs.

Roadmap

1.0.* (TODO)

  • Add docs about manual installation and usage reCAPTCHA with ajax

1.0

  • Initial version