mohsen1369 / yii2-recaptcha-widget
Yii2 Google reCAPTCHA 组件
v1.1.2
2018-02-14 06:26 UTC
Requires
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-21 16:48:19 UTC
README
基于 reCaptcha API 2.0。
安装
安装此扩展的首选方法是通过 composer。
- 运行以下命令之一
php composer.phar require --prefer-dist "himiklab/yii2-recaptcha-widget" "*"
或者
"himiklab/yii2-recaptcha-widget" : "*"
将以下内容添加到您应用程序的 composer.json
文件的 require
部分。
-
在您的配置文件(web.php)中配置组件。siteKey 和 secret 参数是可选的。但如果您省略它们,您需要在每个验证规则和每个希望使用此组件的视图中设置它们。如果在单独的视图或验证规则中设置了 siteKey 或 secret,则会覆盖配置中的设置。
'components' => [ 'reCaptcha' => [ 'name' => 'reCaptcha', 'class' => 'himiklab\yii2\recaptcha\ReCaptcha', 'siteKey' => 'your siteKey', 'secret' => 'your secret key', ], ...
- 在您的模型中添加
ReCaptchaValidator
,例如
public $reCaptcha; public function rules() { return [ // ... [['reCaptcha'], \himiklab\yii2\recaptcha\ReCaptchaValidator::className(), 'secret' => 'your secret key', 'uncheckedMessage' => 'Please confirm that you are not a bot.'] ]; }
或者
public function rules() { return [ // ... [[], \himiklab\yii2\recaptcha\ReCaptchaValidator::className(), 'secret' => 'your secret key'] ]; }
或者简单
public function rules() { return [ // ... [[], \himiklab\yii2\recaptcha\ReCaptchaValidator::className()] ]; }
使用方法
例如
<?= $form->field($model, 'reCaptcha')->widget( \himiklab\yii2\recaptcha\ReCaptcha::className(), ['siteKey' => 'your siteKey'] ) ?>
或者
<?= $form->field($model, 'reCaptcha')->widget(\himiklab\yii2\recaptcha\ReCaptcha::className()) ?>
或者
<?= \himiklab\yii2\recaptcha\ReCaptcha::widget([ 'name' => 'reCaptcha', 'siteKey' => 'your siteKey', 'widgetOptions' => ['class' => 'col-sm-offset-3'] ]) ?>
或者简单
<?= \himiklab\yii2\recaptcha\ReCaptcha::widget(['name' => 'reCaptcha']) ?>