Widget to render google recaptcha and validate this field., (*1)
This implementaion allow to use:
- multiple widgets on one page
- also you can load page by ajax several times, (*2)
, (*3)
Now this widget extends himiklab/yii2-recaptcha-widget extension
(automatic loading as depends in composer.json), (*4)
Installation
Just add extension to composer require section:, (*5)
composer require sashsvamir/yii2-recaptcha-widget:"dev-master"
, (*6)
Setup
Setup recaptcha config common/config/main.php
:, (*7)
'components' => [
// ...
'reCaptcha' => [
'name' => 'reCaptcha',
'class' => 'sashsvamir\yii2\recaptcha\ReCaptcha',
'siteKey' => '<your site public key>',
'secret' => '<your site private key>',
],
],
To debug, use follow keys:, (*8)
'siteKey' => '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI',
'secret' => '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe',
see: https://developers.google.com/recaptcha/docs/faq, (*9)
Using
Add ReCaptchaValidator in your model, for example:, (*10)
public $verifyCode;
public function rules()
{
return [
// ...
['verifyCode', ReCaptchaValidator::className(), 'uncheckedMessage' => 'Please confirm that you are not a bot.',
// add follow lines to prevent checking recaptcha when from has errors
'when' => function ($model) {
return !$model->hasErrors();
}
],
];
}
Render ReCaptcha field:
$form->field($model, 'verifyCode')->widget(\sashsvamir\yii2\recaptcha\ReCaptcha::className())->label(false);