alikdex/yii2-recaptcha

reCaptcha without curl

Installs: 51

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 0

Open Issues: 1

Type:yii2-extension

2.0.0 2018-05-21 12:38 UTC

This package is auto-updated.

Last update: 2024-10-17 09:41:25 UTC


README

Based on google reCaptcha API 2.0.

Installation

The preferred way to install this extension is through composer.

  • Either run
php composer.phar require --prefer-dist "alikdex/yii2-recaptcha" "2.0.0"

or add

"alikdex/yii2-recaptcha" : "2.0.0"

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

  • Sign up for an reCAPTCHA API keys.

  • 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' => [
    'container' => [
        'definitions' => [
            Adx\ReCaptcha\ReCaptcha::class => [
                'siteKey' => 'Your site key',
            ],
            Adx\ReCaptcha\ReCaptchaValidator::class => [
                'secret' => 'Your secret key',
            ],
        ],
    ],
    ...
],
  • Add ReCaptchaValidator in your model, for example:
public $reCaptcha;

public function rules()
{
  return [
      // ...
      [['captcha'], \Adx\ReCaptcha\ReCaptchaValidator::class],
  ];
}

or just

public function rules()
{
  return [
      // ...
      [[], \Adx\ReCaptcha\ReCaptchaValidator::class],
  ];
}

Usage

For example:

<?= \Adx\ReCaptcha\ReCaptcha::widget([
    'name' => 'captcha', // optional
    'widgetOptions' => [
      'class' => 'col-sm-offset-3',
      'data-theme' => 'dark',
      // ...  see google recaptcha2 manual
    ]
]) ?>

or

<?= $form->field($model, 'captcha')->widget(\Adx\ReCaptcha\ReCaptcha::class) ?>

or simply

<?= \Adx\ReCaptcha\ReCaptcha::widget() ?>

Resources