Welcome Guest

Zend Framework 3 / Laminas Google reCaptcha Module

This module simplifies integrating ReCaptcha V2 in your forms.

Installation

With composer
  1. In the root of your application enter:
    $ composer require krytenuk/recaptchav2
Post installation
  1. Enable it in your `application.config.php` or `modules.config.php` file.
    In ZF2 edit your `application.config.php` file:
    <?php
        
    return array(
            
    'modules' => array(
                
    // ...
                
    'FwsReCaptchaV2',
            ),
            
    // ...
        
    );

    In Laminas\ZF3 edit your `modules.config.php` file:
    <?php
        
    return [
            
    'FwsReCaptchaV2',
            
    // ...
        
    ];

Usage

I assume that you are familiar with Laminas Form, if not then please read the docs at https://docs.laminas.dev/.
Firstly you need a Google account, once logged in goto https://www.google.com/recaptcha/admin#list and add your web site and domain name(s) to get your public and private keys.
In your form add the FwsReCaptchaV2 form element in the init() method.
example: <?php

use FwsReCaptchaV2\Form\Element\ReCaptchaV2;

public function 
init()
{
    
// ...
    
$this->add(array(
        
'name' => 'reCaptcha',
        
'type' => FwsReCaptchaV2::class,
        
'options' => array(
            
'label' => _('Verify:'),
            
'label_attributes' => array('class' => 'required'),
            
'pub_key' => 'Your ReCaptcha public key',
        ),
    ));
    
// ...
}
and register the validator in your forms getInputFilterSpecification() method. <?php

use FwsReCaptchaV2\Validator\ReCaptchaV2 as ReCaptchaV2Validator;

public function 
getInputFilterSpecification()
{
    return array(
        
// ...
        
'reCaptcha' => array(
            
'required' => TRUE,
            
'validators' => array(
                array(
                    
'name' => ReCaptchaV2Validator::class,
                    
'options' => array(
                        
'pri_key' => 'Your ReCaptcha private key',
                    ),
                ),
            ),
        ),
        
// ...
    
);
}

Finally to render your the element either use the formRow(), formElement() or formFwsReCaptchaV2() view helper.
example: <?= $this->formFwsReCaptchaV2($this->form->get('reCaptcha')); ?>

The FwsReCaptchaV2\Form\Element\ReCaptchaV2 has the following options.
'pub_key'           Sets your reCaptcha public key (required).
'tag'               The element used to contain the reCaptcha html code.  Currently supports 'div' (default) and 'span'.
're_captcha_class'  Should not need to be changed, defaults to 'g-recaptcha'.
'theme'             The reCaptcha theme, 'light' (default) or 'dark'.
'type'              Sets the reCaptcha type 'image' (default) or 'audio'.
'size'              Set the size of reCaptcha widget, 'normal' (default) or 'compact'
'tabindex'          Set the reCaptcha elements tab index, defaults to 0.
The FwsReCaptchaV2\Validator\ReCaptchaV2 has the following options.
'pri_key'            Sets your reCaptcha private key (required).
'send_ip_address'    Choose to send the users IP address as extra validation, set 'TRUE' (default) or 'FALSE'.
'api_url'            Sets the Google api to perform validation, should not be changed. Defaults to 'https://www.google.com/recaptcha/api/siteverify'.

Donate