EWZRecaptchaBundle
This bundle provides easy reCAPTCHA form field for Symfony., (*1)
Installation
Step 1: Using Composer (recommended)
To install EWZRecaptchaBundle with Composer just add the following to your
composer.json
file:, (*2)
// composer.json
{
// ...
"require": {
// ...
"excelwebzone/recaptcha-bundle": "dev-master"
}
}
NOTE: Please replace dev-master
in the snippet above with the latest stable
branch, for example 2.0.*
., (*3)
Then, you can install the new dependencies by running Composer's update
command from the directory where your composer.json
file is located:, (*4)
$ php composer.phar update
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:, (*5)
registerNamespaces(array(
// ...
'EWZ' => __DIR__.'/../vendor/bundles',
// ...
));
```
Now use the ``vendors`` script to clone the newly added repositories
into your project:
```bash
$ php bin/vendors install
```
### Step 1 (alternative): Using submodules (Symfony 2.0.x)
If you're managing your vendor libraries with submodules, first create the
`vendor/bundles/EWZ/Bundle` directory:
``` bash
$ mkdir -pv vendor/bundles/EWZ/Bundle
```
Next, add the necessary submodule:
``` bash
$ git submodule add git://github.com/excelwebzone/EWZRecaptchaBundle.git vendor/bundles/EWZ/Bundle/RecaptchaBundle
```
### Step2: Configure the autoloader
Add the following entry to your autoloader:
``` php
registerNamespaces(array(
// ...
'EWZ' => __DIR__.'/../vendor/bundles',
));
```
### Step3: Enable the bundle
Finally, enable the bundle in the kernel:
``` php
add('recaptcha', 'ewz_recaptcha');
// ...
}
```
You can pass extra options to reCAPTCHA with the "attr > options" option:
``` php
add('recaptcha', 'ewz_recaptcha', array(
'attr' => array(
'options' => array(
'theme' => 'clean'
)
)
));
// ...
}
```
To validate the field use:
``` php
false``` then the annotation will not work. You have to also set ```constraints```:
``` php
add('recaptcha', 'ewz_recaptcha', array(
'attr' => array(
'options' => array(
'theme' => 'clean'
)
),
'mapped' => false,
'constraints' => array(
new True()
)
));
// ...
```
Cool, now you are ready to implement the form widget:
**PHP**:
``` php
setTheme($form, array('EWZRecaptchaBundle:Form')) ?>
widget($form['recaptcha'], array(
'attr' => array(
'options' => array(
'theme' => 'clean',
),
),
)) ?>
Twig:, (*6)
``` jinja
{% form_theme form 'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig' %}, (*7)
{{ form_widget(form.recaptcha, { 'attr': {
'options' : {
'theme' : 'clean',
},
} }) }}, (*8)
If you are not using a form, you can still implement the reCAPTCHA field
using JavaScript:
**PHP**:
``` php
<div id="recaptcha-container"></div>
<script type="text/javascript">
$(document).ready(function() {
$.getScript("<?php echo \EWZ\Bundle\RecaptchaBundle\Form\Type\RecaptchaType::RECAPTCHA_API_JS_SERVER ?>", function() {
Recaptcha.create("<?php echo $form['recaptcha']->get('public_key') ?>", "recaptcha-container", {
theme: "clean",
});
});
};
</script>
Twig:, (*9)
``` jinja
, (*10)
## Customization
If you want to use a custom theme, put your chunk of code before setting the theme:
``` jinja
{% form_theme form 'EWZRecaptchaBundle:Form:ewz_recaptcha_widget.html.twig' %}
{{ form_widget(form.recaptcha, { 'attr': {
'options' : {
'theme' : 'custom',
},
} }) }}
Further reading: Customizing the Look and Feel of reCAPTCHA, (*11)