Coder Social home page Coder Social logo

yii2-recaptcha-widget's Introduction

Google reCAPTCHA widget for Yii2

Based on Google reCaptcha API 2.0 and 3.0.

Packagist Packagist license

Upgrade to 2.x version

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

Installation

The preferred way to install this extension is through composer.

  • Either run
php composer.phar require --prefer-dist "himiklab/yii2-recaptcha-widget" "*"

or add

"himiklab/yii2-recaptcha-widget" : "*"

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

Usage

For example:

v2

<?= $form->field($model, 'reCaptcha')->widget(
    \himiklab\yii2\recaptcha\ReCaptcha2::className(),
    [
        'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up
    ]
) ?>

v3

<?= $form->field($model, 'reCaptcha')->widget(
    \himiklab\yii2\recaptcha\ReCaptcha3::className(),
    [
        'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up
        'action' => 'homepage',
    ]
) ?>

or

v2

<?= \himiklab\yii2\recaptcha\ReCaptcha2::widget([
    'name' => 'reCaptcha',
    'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up
    'widgetOptions' => ['class' => 'col-sm-offset-3'],
]) ?>

v3

<?= \himiklab\yii2\recaptcha\ReCaptcha3::widget([
    'name' => 'reCaptcha',
    'siteKey' => 'your siteKey', // unnecessary is reCaptcha component was set up
    'action' => 'homepage',
    'widgetOptions' => ['class' => 'col-sm-offset-3'],
]) ?>
  • NOTE: Please disable ajax validation for ReCaptcha field!

Resources

yii2-recaptcha-widget's People

Contributors

anders-akero avatar borales avatar elvenpath avatar eugenekkh avatar ferrumfist avatar himiklab avatar igorrarruda avatar kompaneytsev avatar koteq avatar mauriziocingolani avatar mmlado avatar mohsen1369 avatar necroman avatar p0vidl0 avatar richweber avatar sashsvamir avatar werew01f avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.