PHPackages                             m1roff/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. m1roff/yii2-recaptcha-widget

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

m1roff/yii2-recaptcha-widget
============================

Yii2 Google reCAPTCHA v2 and v3 widget

1.0.1(3y ago)11.3k1MITPHPPHP ^8.0

Since Sep 8Pushed 3y ago1 watchersCompare

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

READMEChangelogDependencies (3)Versions (3)Used By (0)

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

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

Based on Google reCaptcha API 2.0 and 3.0.

Forked version of .

![license](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)

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

[](#installation)

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

- Either run

```
composer require --prefer-dist "m1roff/yii2-recaptcha-widget" "^1.0"

```

- [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' => [
    ReCaptchaConfig::COMPONENT_ID => [
        'class' => ReCaptchaConfig::class,
        'siteKeyV2' => 'your siteKey v2',
        'secretV2' => 'your secret key v2',
        'siteKeyV3' => 'your siteKey v3',
        'secretV3' => 'your secret key v3',
    ],
    ...
```

or use DI container:

```
'container' => [
    'definitions' => [
        m1roff\yii2\recaptcha\ReCaptcha2::class => function ($container, $params, $config) {
            return new m1roff\yii2\recaptcha\ReCaptcha2(
                'your siteKey v2',
                '', // default
                $config
            );
        },
        m1roff\yii2\recaptcha\ReCaptchaValidator2::class => function ($container, $params, $config) {
            return new m1roff\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'], \m1roff\yii2\recaptcha\ReCaptchaValidator2::class,
        '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'], \m1roff\yii2\recaptcha\ReCaptchaValidator3::class,
        'secret' => 'your secret key', // unnecessary if reСaptcha is already configured
        'threshold' => 0.5,
        'action' => 'homepage',
      ],
  ];
}
```

Usage
-----

[](#usage)

For example:

v2

```
