Home >PHP Framework >YII >yii verification code is always wrong

yii verification code is always wrong

王林
王林Original
2020-02-18 11:46:032210browse

yii verification code is always wrong

Problem:

When making a request, I found that I entered the correct verification code, but it was prompted that the verification code was wrong.

Code example:

// 如果这里用到了验证码,就会出问题
 
$model = new Test();
 
$model->validate();
 
$model->save();

(Recommended tutorial: yii framework)

Reason:

If Model is separately validated before saving. After the verification is completed, the verification code will be regenerated. Then when we save the Model, we will also perform validate verification. During verification, the verification code has been regenerated, so it will not match.

Solution:

 
// 这样是正确的
 
$model = new  Test();
// 把需要验证的 attribute 放进去,排除验证码字段
$model->validate(array('test1','test2'));
 
$model->save()

For more programming-related content, please pay attention to the Programming Introduction column on the php Chinese website!

The above is the detailed content of yii verification code is always wrong. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use gii in yiiNext article:How to use gii in yii