Home > PHP Framework > YII > body text

Yii uses rules to get error information

angryTom
Release: 2020-02-17 11:26:13
Original
2541 people have browsed it

Yii uses rules to get error information

yii uses rules to get error information

1. Use

public function rules(){
    return array(
        array('Name,Email','required'),
        array('Email','unique','message'=>'{value}出错啦'),{value}为添加的数据
    );
}
Copy after login
## in model #2. Use

$model = new myModel();//实例化相关表的模型类
$model->attributes = $_POST //对attributes赋值为提交上来(需要验证)的数据,字段为rules中safe
$model->validate();//这里会自动调用验证规则rules
$model->getErrors();//获取所有验证字段的错误信息array('FName'=>array(0 =>'Fname cannot be blank.' ),'字段'=>array(0=>'message'),...)
$model->getErrors('Email');//获取当前传入字段的错误信息array(0 =>'Fname cannot be blank.' )
$model->getError('Email');// 'message信息'
//$form = new CActiveForm();[views中$form=$this->beginWidget('CActiveForm');]
//echo $form->errorSummary($model);打印出所有rules错误信息
Copy after login
in controller Recommended related articles and tutorials:

yii tutorial

The above is the detailed content of Yii uses rules to get error information. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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