Home > PHP Framework > YII > How to get errors in yii model

How to get errors in yii model

Release: 2019-12-17 14:03:03
Original
2757 people have browsed it

How to get errors in yii model

model method to get errors:

public static function getModelError($model) {

    $errors = $model->getErrors();    //得到所有的错误信息

    if(!is_array($errors)) return '';

    $firstError = array_shift($errors);

    if(!is_array($firstError)) return '';

    return array_shift($firstError);

}
Copy after login

getErrors(): Returns errors for all attributes or a single attribute, that is, returns all errors that occurred during validation.

Usage examples are as follows:

$model = new User();

$model->name = $name;

$model->nick_name = $nick_name;

$model->phone = $phone;

$model->sex = $sex;

$model->isNewRecord = true;      //此语句可省略

if(!$model->save()){         //保存不成功

throw new \RuntimeException('保存败.'.$model::getModelError($model));

}
Copy after login

PHP Chinese website has a large number of free Yii introductory tutorials, everyone is welcome to learn!

The above is the detailed content of How to get errors in yii model. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
yii
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template