luyadev/yii2-recaptcha-widget

Yii2 Google reCAPTCHA v2 和 v3 小部件

安装次数: 3,034

依赖项: 1

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 106

类型:yii2-extension

1.0.0 2023-08-16 07:57 UTC

This package is auto-updated.

Last update: 2024-09-09 12:25:23 UTC


README

LUYA Logo

Google reCAPTCHA 小部件

这是从存档的 HIMIKLAB 存储库派生的。主要目标是支持最新的 PHP 版本。

Tests Total Downloads

安装

composer require luyadev/yii2-recaptcha-widget

注册 reCAPTCHA API 密钥 并在配置文件中配置组件。

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

ReCaptchaValidator2ReCaptchaValidator3 添加为模型中的验证器,记得将这些属性设置为必填。

public $reCaptcha;

public function rules()
{
    return [
        [['reCaptcha'], 'required'],
        [['reCaptcha'], \luyadev\recaptcha\ReCaptchaValidator2::class, 'uncheckedMessage' => 'Please confirm that you are not a bot.'],
    ];
}
public $reCaptcha;

public function rules()
{
    return [
        [['reCaptcha'], 'required'],
        [['reCaptcha'], \luyadev\recaptcha\ReCaptchaValidator3::class, 'threshold' => 0.5, 'action' => 'homepage'],
    ];
}

在 ActiveForm 视图文件中的使用

注意:禁用 ReCaptcha 字段的 Ajax 验证!

$form->field($model, 'reCaptcha')->widget(\luyadev\recaptcha\ReCaptcha2::class) // v2
$form->field($model, 'reCaptcha')->widget(\luyadev\recaptcha\ReCaptcha3::class, ['action' => 'homepage']) // v3

作为小部件

\luyadev\recaptcha\ReCaptcha2::widget([
    'name' => 'reCaptcha',
    'widgetOptions' => ['class' => 'col-sm-offset-3'],
]);

v3

\luyadev\recaptcha\ReCaptcha3::widget([
    'name' => 'reCaptcha',
    'action' => 'homepage',
    'widgetOptions' => ['class' => 'col-sm-offset-3'],
]);