Php yii2验证码验证无效
我正在尝试将验证码添加到我的Php yii2验证码验证无效,php,yii2,captcha,Php,Yii2,Captcha,我正在尝试将验证码添加到我的yii2应用程序的登录页面。 我试过一些教程,问题是验证码总是正确的,就像没有验证一样 我试过这个: 我的代码是: 登录表单 public $username; public $password; public $rememberMe = true; private $_user = false; public $captcha; // add this varible to your model class. /** * @retur
yii2
应用程序的登录页面。
我试过一些教程,问题是验证码总是正确的,就像没有验证一样
我试过这个:
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'],
];
}
站点控制器
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>
我知道这个话题很老,但可能是你的url规则有问题。您是否尝试添加:
'captcha'=>site/captcha'
在您的配置文件的url部分中,我在我的项目中尝试了您的代码-在localhost上一切正常。渲染后,是否可以检查capcha刷新和验证规则的请求url。您可以显示控制器的actionLogin吗?公共函数actionLogin(){$this->layout=false;if(!Yii::$app->user->isGuest){return$this->goHome();}$model=new LoginForm();if($model->load(Yii::$app->request->post())和&$model->login(){return$this->goBack();}$model->password='';返回$this->render('login',['model'=>$model,]);}@FeR-S是否尝试输入错误的验证码?是的,验证工作正常。渲染后是否有js规则验证?你能检查一下吗?这应该在
之前看到是的,我把代码放在前面,我的意思是第一次,我已经把代码放在@NurDiarto之前了。好的,它没有指定,所以答案中指定了它。现在可以用了吗?您是否用此代码替换了您的代码作为回答?是的,我尝试过替换代码,但仍然不起作用