luyadev/yii2-recaptcha-widget

Yii2 Google reCAPTCHA v2 and v3 widget

Installs: 3 865

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 106

Type:yii2-extension

1.0.0 2023-08-16 07:57 UTC

This package is auto-updated.

Last update: 2024-11-09 12:52:45 UTC


README

LUYA Logo

Google reCAPTCHA widget

THIS IS A FORK FROM THE ARCHIVED HIMIKLAB REPOSITORY. THE MAIN GOAL IS TO SUPPORT LATEST PHP VERSIONS

Tests Total Downloads

Installation

composer require luyadev/yii2-recaptcha-widget

Sign up for an reCAPTCHA API keys and configure the component in your configuration file.

'components' => [
    'reCaptcha' => [
        'class' => 'luyadev\recaptcha\ReCaptchaConfig',
        'siteKeyV2' => 'your siteKey v2',
        'secretV2' => 'your secret key v2',
        'siteKeyV3' => 'your siteKey v3',
        'secretV3' => 'your secret key v3',
    ],
    ...

Add ReCaptchaValidator2 or ReCaptchaValidator3 as validator into your model, do not forget to set those attributes as required.

public $reCaptcha;

public function rules()
{
    return [
        [['reCaptcha'], 'required'],
        [['reCaptcha'], \luyadev\recaptcha\ReCaptchaValidator2::class, 'uncheckedMessage' => 'Please confirm that you are not a bot.'],
    ];
}
public $reCaptcha;

public function rules()
{
    return [
        [['reCaptcha'], 'required'],
        [['reCaptcha'], \luyadev\recaptcha\ReCaptchaValidator3::class, 'threshold' => 0.5, 'action' => 'homepage'],
    ];
}

Usage in the view files for ActiveForm:

NOTE: Disable ajax validation for ReCaptcha field!

$form->field($model, 'reCaptcha')->widget(\luyadev\recaptcha\ReCaptcha2::class) // v2
$form->field($model, 'reCaptcha')->widget(\luyadev\recaptcha\ReCaptcha3::class, ['action' => 'homepage']) // v3

as widgets:

\luyadev\recaptcha\ReCaptcha2::widget([
    'name' => 'reCaptcha',
    'widgetOptions' => ['class' => 'col-sm-offset-3'],
]);

v3

\luyadev\recaptcha\ReCaptcha3::widget([
    'name' => 'reCaptcha',
    'action' => 'homepage',
    'widgetOptions' => ['class' => 'col-sm-offset-3'],
]);