brussens/yii2-recaptcha

Google ReCaptcha v2.0 Yii 2.x.x extension

Installs: 20 295

Dependents: 0

Suggesters: 0

Security: 0

Stars: 6

Watchers: 4

Forks: 5

Open Issues: 3

Type:yii2-extension

1.1.0 2018-08-26 03:27 UTC

This package is auto-updated.

Last update: 2024-11-15 01:56:58 UTC


README

Latest Stable Version Total Downloads License

The main difference from the similar extensions is that this one doesn't require an application component with constant name, such as \Yii::$app->recaptcha or something.

Install

Either run

php composer.phar require --prefer-dist brussens/yii2-recaptcha "*"

or add

"brussens/yii2-recaptcha": "*"

to the require section of your composer.json file.

Add to your bootstrap file:

$container->setSingleton(\ReCaptcha\ReCaptcha::class, function($container, $params, $config) {
    return new \ReCaptcha\ReCaptcha('your secret');
});

$container->set(\brussens\yii2\extensions\recaptcha\Widget::class, function($container, $params, $config) {
    return new \brussens\yii2\extensions\recaptcha\Widget('your site key', \Yii::$app->language, $config);
});

Since Yii 2.0.11 you can also configure the container in the 'container' section of the app configuration:

'container' => [
    'definitions' => [
        \brussens\yii2\extensions\recaptcha\Widget::class => function($container, $params, $config) {
            return new \brussens\yii2\extensions\recaptcha\Widget('your site key', \Yii::$app->language, $config);
        }
    ],
    'singletons' => [
         \ReCaptcha\ReCaptcha::class => function($container, $params, $config) {
             return new \ReCaptcha\ReCaptcha('your secret');
         }
    ]
]

Add in your model validation rules

public function rules()
{
    return [
        ...
        ['verifyCode', \brussens\yii2\extensions\recaptcha\Validator::className()],
        ...
    ];
}

Add in your view

echo $form->field($model, 'verifyCode')->widget(\brussens\yii2\extensions\recaptcha\Widget::className());

If you use Pjax or multiple widgets on page

echo $form->field($model, 'verifyCode')->widget(
    \brussens\yii2\extensions\recaptcha\Widget::className(), [
    'options' => [
        'id' => 'insert-unique-widget-id'
    ]
]);