yii uses rules to get error information
1. Use
public function rules(){ return array( array('Name,Email','required'), array('Email','unique','message'=>'{value}出错啦'),{value}为添加的数据 ); }
$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错误信息
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!