Yii Framework Forum: Google reCAPTCHA V2 widget for Yii 1 - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Google reCAPTCHA V2 widget for Yii 1 Rate Topic: -----

#1 User is offline   cheerymagic 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 13-October 17

Posted 13 October 2017 - 01:46 PM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users