D:\phpwork\news\controllers\SiteController.php
public function actionRegister()
{
$model = new RegisterForm();
if ($model->load(Yii::$app->request->post())) {
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$result=\yii\widgets\ActiveForm::validate($model);
return $result;
}
if($model->validate()){
$error=$model->save();
if($error) {
return $this->errorDisplay($error);
}else{
Yii::$app->session->setFlash('success');
return $this->refresh();
}
}else{
return $this->errorDisplay($model->getErrors());
}
}else{
return $this->render('register', [
'model' => $model,
]);
}
}
D:\phpwork\news\models\RegisterForm.php
['verifyCode', 'captcha', 'captchaAction'=>'site/captcha'],
报错信息:
The verification code is incorrect.
个人感觉是下面这段Ajax验证代码运行后,verifyCode就被自动刷新了:
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$result=\yii\widgets\ActiveForm::validate($model);
return $result;
}
请问大神们是怎么解决的这个问题?谢谢
` use yii\captcha\CaptchaAction;
[‘verifyCode’, ‘captcha’,’message’=>’验证码不正确’],
而不是这么写的
[‘verifyCode’, ‘captcha’, ‘captchaAction’=>’site/captcha’],
我将rules的规则[ 'coder','captcha', 'captchaAction'=>'coder/captcha','message'=>"{attribute}错误"]
给删除了,已经用ajax验证,就不进行再次验证了,直接提交表单的时候就可以通过了。