demi/recaptcha

Yii2 wrapper for reCAPTCHA widget

1.0.1 2015-08-03 10:55 UTC

This package is auto-updated.

Last update: 2024-10-27 21:24:37 UTC


README

Yii2 wrapper for [reCAPTCHA](https://www.google.com/recaptcha/intro/index.html widget)

Installation

Run

composer require "demi/recaptcha" "~1.0"

Configurations

Register new reCAPTCHA API keys

Add keys to /common/config/params.php:

return [
    // reCAPTCHA API keys
    'reCAPTCHA.siteKey' => 'xxxxxxxxx',
    'reCAPTCHA.secretKey' => 'xxxxxxxxx',
];

In the form:

<?= $form->field($model, 'captcha', ['enableAjaxValidation' => false])->label(false)
    ->widget('demi\recaptcha\ReCaptcha', ['siteKey' => Yii::$app->params['reCAPTCHA.siteKey']]) ?>

In the model validation rules:

public function rules()
{
    return [
        // captcha
        [
            ['captcha'], 'demi\recaptcha\ReCaptchaValidator', 'secretKey' => Yii::$app->params['reCAPTCHA.secretKey'],
            'when' => function ($model) {
                /** @var $model self */
                return !$model->hasErrors() && Yii::$app->user->isGuest;
            }
        ],
}