Skip to content

Latest commit

 

History

History
115 lines (86 loc) · 2.74 KB

README.md

File metadata and controls

115 lines (86 loc) · 2.74 KB

Provides use reCAPTCHA as form field.

Installation

Add RecaptchaBundle to your application kernel:

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new EWZ\Bundle\RecaptchaBundle\EWZRecaptchaBundle(),
        // ...
    );
}

Add the EWZ namespace to your autoloader:

// app/autoload.php
$loader->registerNamespaces(array(
    // ...
    'EWZ' => __DIR__.'/../src',
    // ...
));

Add your private and public key for reCAPTCHA in configuration file:

// app/config/config.yml
// ...
ewz_recaptcha:
    pubkey:   here_is_your_publick_key
    privkey:  here_is_your_private_key
    secure:   true

Note: If you use secure url for reCAPTCHA put true in secure.

Use in forms

Add the following lines to your form class:

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

To validate the field use:

use EWZ\Bundle\RecaptchaBundle\Validator\Constraints as Recaptcha;

/**
 * @Recaptcha\True
 */
public $recaptcha;

Use in view

Display field using form widget:

PHP (Note: this is still under development):

<?php echo $view['form']->widget($form['recaptcha'], array(
    'attr' => array(
        'options' => array(
            'theme' => 'clean',
        ),
    ),
), array(
    'theme' => 'EWZRecaptchaBundle:Form:recaptcha_widget.html.php',
)) ?>

Twig:

{% form_theme form 'EWZRecaptchaBundle:Form:recaptcha_widget.html.twig' %}

{{ form_widget(form.recaptcha, { 'attr': {
    'options' : {
        'theme' : 'clean',
    },
} }) }}

Or using JavaScript:

PHP:

<div id="recaptcha-container"></div>
<script type="text/javascript">
    $(document).ready(function() {
        $.getScript("<?php echo \EWZ\Bundle\RecaptchaBundle\Form\Extension\Core\Type\RecaptchaType::RECAPTCHA_API_JS_SERVER ?>", function() {
            Recaptcha.create("<?php echo $form['recaptcha']->get('pubkey') ?>", "recaptcha-container", {
                theme: "clean",
            });
        });
    };
</script>

Twig:

<div id="recaptcha-container"></div>
<script type="text/javascript">
    $(document).ready(function() {
        $.getScript("{{ constant('\\EWZ\\Bundle\\RecaptchaBundle\\Form\\Extension\\Core\\Type\\RecaptchaType::RECAPTCHA_API_JS_SERVER') }}", function() {
            Recaptcha.create("{{ form.recaptcha.get('pubkey') }}", "recaptcha-container", {
                theme: "clean"
            });
        });
    });
</script>