Home > Article > PHP Framework > yii prompts an error when entering the correct verification code
When making a request, I found that the correct verification code was entered, but the verification code error was prompted.
Finally, following the code, we found that if the Model is validated separately before saving, the verification code will be regenerated after the verification is completed. Then when we Model save
, we will also perform validate verification. During verification, the verification code has been regenerated, so it will not match.
Problem discovery:
We can look at framework/web/widgets/captcha/CCaptchaAction.php
and we can easily find the problem.
<?php class CaptchaAction extends CCaptchaAction{ public function validate($input, $caseSensitive) { $code = $this->getVerifyCode(); $valid = $caseSensitive ? ($input === $code) : !strcasecmp($input, $code); $session = Yii::app()->session; $session->open(); $name = $this->getSessionKey() . 'count'; if (!Yii::app()->request->isAjaxRequest) { $session[$name] = $session[$name] + 1; } // 这里会重新生成 if ($session[$name] > $this->testLimit && $this->testLimit > 0) { $this->getVerifyCode(true); } return $valid; } }
// 如果这里用到了验证码,就会出问题$model = new Test(); $model->validate(); $model->save();
// 这样是正确的$model = new Test(); // 把需要验证的 attribute 放进去,排除验证码字段 $model->validate(array('test1','test2')); $model->save()
Recommended related articles and tutorials: yii tutorial
The above is the detailed content of yii prompts an error when entering the correct verification code. For more information, please follow other related articles on the PHP Chinese website!