PHPackages                             gilbertoteles/yii2-recaptcha-widget - 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. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. gilbertoteles/yii2-recaptcha-widget

ActiveYii2-extension[Authentication &amp; Authorization](/categories/authentication)

gilbertoteles/yii2-recaptcha-widget
===================================

Yii2 Google reCAPTCHA v2 and v3 widget

0178PHP

Since Jul 29Pushed 3y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Google reCAPTCHA widget for Yii2
================================

[](#google-recaptcha-widget-for-yii2)

Based on Google reCaptcha API 2.0 and 3.0.

FORKED from himiklab/yii2-recaptcha-widget that was abandoned - THAKS to himiklab 4all!

![Packagist](https://camo.githubusercontent.com/ccba3ae8363be77a7abda6bbbb020869f93040d4018e3465898de6c0ce1c4629/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f67696c626572746f74656c65732f796969322d7265636170746368612d7769646765742e737667) ![Packagist](https://camo.githubusercontent.com/a04501db0abd57d2a5730284a472d1c3854bade762458ea07a867799cc007f0a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f67696c626572746f74656c65732f796969322d7265636170746368612d7769646765742e737667) ![license](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)

Upgrade to 2.x version
----------------------

[](#upgrade-to-2x-version)

Warning! Classes `ReCaptcha` and `ReCaptchaValidator` is deprecated. Please replace their to `ReCaptchaConfig`, `ReCaptcha2` and `ReCaptchaValidator2`.

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

- Either run

```
php composer.phar require --prefer-dist "gilbertoteles/yii2-recaptcha-widget" "*"

```

or add

```
"gilbertoteles/yii2-recaptcha-widget" : "*"
```

to the `require` section of your application's `composer.json` file.

- [Sign up for an reCAPTCHA API keys](https://www.google.com/recaptcha/admin/create).
- Configure the component in your configuration file (web.php). The parameters siteKey and secret are optional. But if you leave them out you need to set them in every validation rule and every view where you want to use this widget. If a siteKey or secret is set in an individual view or validation rule that would overrule what is set in the config.

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

or use DI container:

```
'container' => [
    'definitions' => [
        gilbertoteles\yii2\recaptcha\ReCaptcha2::className() => function ($container, $params, $config) {
            return new gilbertoteles\yii2\recaptcha\ReCaptcha2(
                'your siteKey v2',
                '', // default
                $config
            );
        },
        gilbertoteles\yii2\recaptcha\ReCaptchaValidator2::className() => function ($container, $params, $config) {
            return new gilbertoteles\yii2\recaptcha\ReCaptchaValidator2(
                'your secret key v2',
                '', // default
                null, // default
                null, // default
                $config
            );
        },
    ],
],
```

- Add `ReCaptchaValidator2` or `ReCaptchaValidator3` in your model, for example:

v2

```
public $reCaptcha;

public function rules()
{
  return [
      // ...
      [['reCaptcha'], \gilbertoteles\yii2\recaptcha\ReCaptchaValidator2::className(),
        'secret' => 'your secret key', // unnecessary if reСaptcha is already configured
        'uncheckedMessage' => 'Please confirm that you are not a bot.'],
  ];
}
```

v3

```
public $reCaptcha;

public function rules()
{
  return [
      // ...
      [['reCaptcha'], \gilbertoteles\yii2\recaptcha\ReCaptchaValidator3::className(),
        'secret' => 'your secret key', // unnecessary if reСaptcha is already configured
        'threshold' => 0.5,
        'action' => 'homepage',
      ],
  ];
}
```

Usage
-----

[](#usage)

For example:

v2

```
