sashsvamir/yii2-recaptcha-widget

Yii2 Google reCAPTCHA widget

dev-master 2018-12-27 14:34 UTC

This package is not auto-updated.

Last update: 2024-04-14 03:12:12 UTC


README

Widget to render google recaptcha and validate this field.

This implementaion allow to use:

  • multiple widgets on one page
  • also you can load page by ajax several times

recaptcha

Information

Now this widget extends himiklab/yii2-recaptcha-widget extension (automatic loading as depends in composer.json)

Installation

Just add extension to composer require section:

composer require sashsvamir/yii2-recaptcha-widget:"dev-master"

Setup

Setup recaptcha config common/config/main.php:

'components' => [
    // ...
    'reCaptcha' => [
        'name' => 'reCaptcha',
        'class' => 'sashsvamir\yii2\recaptcha\ReCaptcha',
        'siteKey' => '<your site public key>',
        'secret' => '<your site private key>',
    ],
],

To debug, use follow keys:

'siteKey' => '6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI',
'secret' => '6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe',

see: https://developers.google.com/recaptcha/docs/faq

Using

Add ReCaptchaValidator in your model, for example:

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);