Google reCAPTCHA V2 widget for Yii 1

Google reCAPTCHA V2 widget for Yii 1

Installation

Get API key from Google.www.google.com/recaptcha/admin#createsite

Copy Source Folder to Extensions

Set Public Key (siteKey) and Private Key (secret) in params or components

Add SReCaptchaValidator in your model:


public $reCaptcha;

//...


public function rules()

{

  return [

      // ...

      // secret is required

      ['reCaptcha', 'application.extensions.reCaptcha2.SReCaptchaValidator', 'secret' => Yii::app()->params['reCaptcha2PrivateKey'],'message' => 'The verification code is incorrect.']

      // ...

  ];

}

Usage

If you want render ReCaptcha use simple code in your view file:


Yii::import('application.extensions.reCaptcha2.SReCaptcha');

$this->widget('SReCaptcha',

  [

        'name' => 'reCaptcha', //is requred

        'siteKey' => Yii::app()->params['reCaptcha2PublicKey'], //is requred

        'widgetOptions' => ['class' => 'col-lg-3'],

        'theme' => SReCaptcha::THEME_DARK,

        'size' => SReCaptcha::SIZE_COMPACT,

        'jsCallback' => 'console.log("reCaptcha is ready!");console.log(response);',

        'jsExpiredCallback' => 'console.log("reCaptcha is expired!");'

  ]

);

If you want render ReCaptcha in active form:


$form->widget('application.extensions.reCaptcha2.SReCaptcha',

    [

        'name' => 'reCaptcha', //is requred

        'siteKey' => Yii::app()->params['reCaptcha2PublicKey'], //is requred

        'model' => $model,

        //'attribute' => 'reCaptcha' //if we use model name equal attribute or customize attribute

    ]

);

If you use several forms or ajax and need multi render ReCaptchas:


<div id="reCaptchaLogin"></div>

<div id="reCaptchaReg"></div>


$this->widget('application.extensions.reCaptcha2.SReCaptcha',

    [

        'siteKey' => Yii::app()->params['reCaptcha2PublicKey'],

        'render' => [

            'reCaptchaLogin' => [

                'callback' => 'console.log("#reCaptchaLogin is ready!");',

                'theme' => 'dark',

                'expired-callback' => 'console.log("#reCaptchaLogin is expired!");'

            ],

            'reCaptchaReg' => [

                'size' => 'compact',

                'callback' => 'console.log("#reCaptchaReg is ready!");console.log(response);',

            ]

        ],

    ]

);

More information:

developers.google.com/recaptcha/docs/display.

developers.google.com/recaptcha/docs/verify.

github.com/cheerymagicsoftware/Yii1ReCaptcha2.