转载http://blog.sina.com.cn/s/blog_6dcc46a60101c0f2.html
model中使用
public function rules(){
return array(
array(‘Name,Email‘,‘required‘),
array(‘Email‘,‘unique‘,‘message‘=>‘{value}出错啦‘),{value}为添加的数据
);
}
controller中使用
$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错误信息
时间: 2024-11-12 00:45:51