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