PHPackages                             manchenkov/yii2-recaptcha3 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. manchenkov/yii2-recaptcha3

AbandonedArchivedYii2-extension[Templating &amp; Views](/categories/templating)

manchenkov/yii2-recaptcha3
==========================

Yii 2 extension to work with Google reCAPTCHA v3

1.0.3(5y ago)319.8k↓47.2%2MITPHP

Since May 30Pushed 3y ago1 watchersCompare

[ Source](https://github.com/manchenkoff/yii2-recaptcha3)[ Packagist](https://packagist.org/packages/manchenkov/yii2-recaptcha3)[ RSS](/packages/manchenkov-yii2-recaptcha3/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (5)Used By (0)

Yii 2 reCAPTCHA v3
==================

[](#yii-2-recaptcha-v3)

This package is a simple extension for Yii 2 which helps to work with [Google reCAPTCHA v3](https://developers.google.com/recaptcha/)

- [Installation](#installation)
- [Usage](#usage)
    - [Basic](#basic-setup)
    - [AJAX validation](#ajax-validation)
    - [Twig integration](#usage-with-twig)
- [Localization](#localization)
- [Hiding Google reCaptcha badge](#hiding-google-privacy-badge)

Installation
------------

[](#installation)

You have to run the following command to add a dependency to your project

```
composer require manchenkov/yii2-recaptcha3
```

or you can add this line to `require` section of `composer.json`

```
"manchenkov/yii2-recaptcha3": "^1.0.3"

```

Usage
-----

[](#usage)

### Basic setup

[](#basic-setup)

In first, you have to add the following settings into your application **params** section (usually `params.php` file).

```
return [
    // your other params
    'reCAPTCHA.siteKey' => 'GOOGLE_RECAPTCHA_SITE_KEY',
    'reCAPTCHA.secretKey' => 'GOOGLE_RECAPTCHA_SECRET_KEY',
];

```

Then you should add the rules as below to validate the model

```
public $captcha;

public function rules()
{
    return [
        // other validation rules
        ['captcha', ReCaptchaValidator::class, 'score' => 0.8, 'action' => 'login'],
    ];
}

```

- **score**: minimal value of acceptance score (default: **0.5**)
- **action**: page action name (default: **homepage**)
- **message**: error message to display (see **Localization** section below)

The last step is an adding ActiveForm input, just use the next example:

```
$form->field($model, 'captcha')->widget(ReCaptchaWidget::class);

// or

$form->field($model, 'captcha')->widget(ReCaptchaWidget::class, ['action' => 'login']);
// action name must be the same as validation rules

```

### AJAX Validation

[](#ajax-validation)

Default behaviour may cause some problems with AJAX validation because Google recommends to request captcha token just before submitting form, see documentation

```
Note: reCAPTCHA tokens expire after two minutes. If you're protecting an action with reCAPTCHA, make sure to call execute when the user takes the action rather than on page load.

```

In this case Yii controller will receive the empty value and give you validation error.

To solve this problem **ReCaptchaWidget** supports additional attribute - **preloading**'.

You should use the following example in your views files

```

...
