EWZRecaptchaBundle ================== [![Build Status](https://api.travis-ci.org/excelwebzone/EWZRecaptchaBundle.svg)](https://travis-ci.org/excelwebzone/EWZRecaptchaBundle) This bundle provides easy reCAPTCHA form field for Symfony. A bridge for the Silex framework has been implemented too : [Jump to documentation](Bridge/README.md). ## Installation ### Step 1: Use composer and enable Bundle To install EWZRecaptchaBundle with Composer just type in your terminal: ```bash php composer.phar require excelwebzone/recaptcha-bundle ``` Now, Composer will automatically download all required files, and install them for you. All that is left to do is to update your ``AppKernel.php`` file, and register the new bundle: ```php add('recaptcha', EWZRecaptchaType::class); // ... } ``` > *Note that in Symfony versions lower than 2.8 refers to form types by name instead of class name, use:* > > ``` php > > public function buildForm(FormBuilder $builder, array $options) > { > // ... > $builder->add('recaptcha', 'ewz_recaptcha'); > // ... > } > ``` You can pass extra options to reCAPTCHA with the "attr > options" option: ``` php add('recaptcha', EWZRecaptchaType::class, array( 'attr' => array( 'options' => array( 'theme' => 'light', 'type' => 'image', 'size' => 'normal', 'defer' => true, 'async' => true, ) ) )); // ... } ``` Support Google's Invisible is super easy: ``` php add('recaptcha', EWZRecaptchaType::class, array( 'attr' => array( 'options' => array( 'theme' => 'light', 'type' => 'image', 'size' => 'invisible', // set size to invisible 'defer' => true, 'async' => true, 'callback' => 'onReCaptchaSuccess', // callback will be set by default if not defined (along with JS function that validate the form on success) 'bind' => 'btn_submit', // this is the id of the form submit button // ... ) ) )); // ... } ``` > Note: If you use the pre-defined callback, you would need to add `recaptcha-form` class to your `
` tag. If you need to configure the language of the captcha depending on your site language (multisite languages) you can pass the language with the "language" option: ``` php add('recaptcha', EWZRecaptchaType::class, array( 'language' => 'en' // ... )); // ... } ``` To validate the field use: ``` php false``` then the annotation will not work. You have to also set ```constraints```: ``` php add('recaptcha', EWZRecaptchaType::class, array( 'attr' => array( 'options' => array( 'theme' => 'light', 'type' => 'image', 'size' => 'normal' ) ), 'mapped' => false, 'constraints' => array( new RecaptchaTrue() ) )); // ... ``` The form template resource is now auto registered via an extension of the container. However, you can always implement your own custom form widget. **PHP**: ``` php setTheme($form, array('EWZRecaptchaBundle:Form')) ?> widget($form['recaptcha'], array( 'attr' => array( 'options' => array( 'theme' => 'light', 'type' => 'image', 'size' => 'normal' ), ), )) ?> ``` **Twig**: ``` jinja {% form_theme form 'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig' %} {{ form_widget(form.recaptcha, { 'attr': { 'options' : { 'theme': 'light', 'type': 'image', 'size': 'normal' }, } }) }} ``` If you are not using a form, you can still implement the reCAPTCHA field using JavaScript: **PHP**: ``` php
``` **Twig**: ``` jinja
``` ## Customization If you want to use a custom theme, put your chunk of code before setting the theme: ``` jinja
Incorrect please try again
Enter the words above: Enter the numbers you hear:
Get another CAPTCHA
Help
{% form_theme form 'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig' %} {{ form_widget(form.recaptcha, { 'attr': { 'options' : { 'theme' : 'custom', }, } }) }} ```