我正在尝试将验证码添加到我的yii2
应用程序的登录页面上。我已经尝试过一些教程,问题是验证码总是像没有验证一样是正确的。
我已经尝试过:
我的代码是:
LoginForm
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
public $captcha; // add this varible to your model class.
/**
* @return array the validation rules.
*/
public function rules() {
return [
// username and password are both required
[['username', 'password','captcha'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
['captcha', 'captcha','captchaAction'=>'/site/captcha'], // add this code to your rules.
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
SiteController
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
查看login.php
<div class="form-goup">
<?= $form->field($model, 'captcha')->widget(yii\captcha\Captcha::className(), '
['template' => '<div class="row"><div class="col-lg-3" style="margin-right:25px;">{image}</div><div class="col-lg-6">{input}</div></div>',
]); ?>
</div>
您的代码有效,但是您有语法错误,一个字母(这只是修饰性的)和一个多余的',这是完全错误的:
<div class="form-group">
<?= $form->field($model, 'captcha')->widget(yii\captcha\Captcha::className(), ['template' => '<div class="row"><div class="col-lg-3" style="margin-right:25px;">{image}</div><div class="col-lg-6">{input}</div></div>',]); ?>
</div>
应该在提交按钮 <?php ActiveForm::end();
?>
我知道该主题很旧,但是可能是您的网址规则所致。您是否尝试过添加:
'captcha'=>site/captcha'
在您的配置文件中,网址部分