Yii2 wrapper for [reCAPTCHA](https://www.google.com/recaptcha/intro/index.html widget)
Run
composer require "demi/recaptcha" "~1.0"
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;
}
],
}
Comments