xorock/zend-service-recaptcha-v2

Zend Framework 3 service for Google ReCaptcha v2

0.1.0 2016-11-05 07:58 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:27:31 UTC


README

#Zend Framework 3 integration with Google ReCaptcha v2

Provides Google ReCaptcha v2 integration for Zend Framework 3.

Installation

Install this library using composer:

$ composer require xorock/zend-service-recaptcha-v2

Then add ZfServiceReCaptcha2 to Your module config under the modules key.

Using Zend Framework Captcha element

use Zend\Form\Element\Captcha;
use ZfServiceReCaptcha2\Captcha\ReCaptcha2;

$this->add([
    'type'       => Captcha::class,
    'name'       => 'g-recaptcha-response', // name is required for element to be validated
    'options'    => [
        'label' => 'Please answer question',
        'captcha' => [
            'class' => ReCaptcha2::class,
            'options' => [
                'hl' => 'en', // english is set by deafult, this line is not required
                'theme' => 'light', // see options below
                'callback' => '', // callback function, etc.
                'public_key'  => 'Generated public key',
                'private_key' => 'Generated private key'
            ],
        ],
    ],
]);

Options

Form element allows two different type of options: params and attributes. Both refer to https://developers.google.com/recaptcha/docs/display configuration options. Parameters are published inside 'script' tag, while the attributes referes to 'div.g-recaptcha element'. By default they are defined as:

/**
 * Parameters for the script object
 *
 * @var array
 */
protected $params = array(
    'onload' => null,
    'render' => 'onload',
    'hl'     => 'en'
);
    
/**
 * Attributes for div element
 *
 * @var array
 */
protected $attributes = array(
    'class'            => 'g-recaptcha',
    'theme'            => 'light',
    'type'             => 'image',
    'tabindex'         => 0,
    'callback'         => null,
    'expired-callback' => null
);

ReCaptcha2 form element

ZfServiceReCaptcha2 component also comes with a predefined element ReCaptcha2, which extends built-in \Zend\Form\Element\Captcha. By default, it consumes following config:

return [
    'zfservicerecaptcha2' => [
        'recaptcha' => [
            'options' => [
                // Captcha options
                'hl' => 'en',
                'public_key'  => 'Generated public key',
                'private_key' => 'Generated private key'
            ],
        ]
    ]
];

It is convenient to set Your ReCaptcha keys and other options in general application configuration. You can use this element by simply defining

use ZfServiceReCaptcha2\Form\Element\ReCaptcha2;

$this->add([
    'type'       => ReCaptcha2::class,
    // Field name is defined by factory
    // 'name'       => 'g-recaptcha-response', 
    'options'    => [
        'label' => 'Please answer question',
    ],
]);