luyadev / yii2-recaptcha-widget
Yii2 Google reCAPTCHA v2 和 v3 小部件
1.0.0
2023-08-16 07:57 UTC
Requires
- yiisoft/yii2: ^2.0.11
- yiisoft/yii2-httpclient: *
Requires (Dev)
- luyadev/luya-testsuite: ^3.0
This package is auto-updated.
Last update: 2024-09-09 12:25:23 UTC
README
Google reCAPTCHA 小部件
这是从存档的 HIMIKLAB 存储库派生的。主要目标是支持最新的 PHP 版本。
安装
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', ], ...
将 ReCaptchaValidator2
或 ReCaptchaValidator3
添加为模型中的验证器,记得将这些属性设置为必填。
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'], ]);