This is a fork updated from https://github.com/dmishh/RecaptchaBundle
- 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
-
Add the following to your
composer.json
file:// composer.json { // ... "require": { // ... "dmishh/recaptcha-bundle": "1.0.*" } }
-
Update dependencies, run from command line:
php composer.phar update
-
Register the bundle in your
AppKernel.php
file:<?php // in AppKernel::registerBundles() $bundles = array( // ... new Dmishh\Bundle\RecaptchaBundle\RecaptchaBundle() );
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
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.
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()
));
}
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.
- Add docs about manual installation and usage reCAPTCHA with ajax
- Initial version