Home > php教程 > php手册 > Yii CModel中rules验证 获取错误信息,yiicmodel

Yii CModel中rules验证 获取错误信息,yiicmodel

WBOY
Release: 2016-06-13 09:27:22
Original
1455 people have browsed it

Yii CModel中rules验证 获取错误信息,yiicmodel

<span>在model中定义
</span><span>public</span> <span>function</span><span> rules(){
        </span><span>return</span> <span>array</span><span>(
            </span><span>array</span>('Name,Email','required'),
            <span>array</span>('Email','unique','message'=>'{value}出错啦'),//<span>{value}为添加的数据
        );
    }
controller中使用
</span><span>$model</span> = <span>new</span> myModel();<span>//</span><span>实例化相关表的模型类</span>
<span>$model</span>->attributes = <span>$_POST</span> <span>//</span><span>对attributes赋值为提交上来(需要验证)的数据,字段为rules中safe</span>
<span>$model</span>->validate();<span>//</span><span>这里会自动调用验证规则rules</span>
<span>$model</span>->getErrors();<span>//</span><span>获取所有验证字段的错误信息</span>
<span>$model</span>->getErrors('Email');<span>//</span><span>获取当前传入字段的错误信息</span>
<span>$model</span>->getError('Email');<span>//</span><span> 'message信息'
</span>
Copy after login

 

YII怎获取错误的表单信息并保存到字符串里

这个用
CHtml::errorSummary($model1)
你可以查一下手册 他返回就是字符串了
 

yii表单出错信息在哪个文件里

你的问题描述的不是很清楚,我就给你说下Yii表单验证吧!
Yii表单验证你可以参考一下它的demos中的blog的SiteController的actionContact方法:
public function actionContact()
{
$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
$model->attributes=$_POST['ContactForm'];
if($model->validate())
{
$headers="From: {$model->email}\r\nReply-To: {$model->email}";
mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
$this->refresh();
}
}
$this->render('contact',array('model'=>$model));
}
这里模型那部分我就不列出来了,自己看它的demo里面的吧!
这个方法就是表单提交之后
if(isset($_POST['ContactForm']))判断表单是否提交状态的

$model->attributes=$_POST['ContactForm'];将表单数据赋值给model属性

if($model->validate()) 这一步就是调用model里面的验证,如果有错误就会返回错误,这样就会重新加载视图,这样视图里面显示表单错误部分就会有错输出!

errorSummary($model); ?>

这部分来自视图里面错误输出部分!
 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template