Yii CModel中rules驗證 擷取錯誤資訊,yiicmodel_PHP教程

來源:互聯網
上載者:User

Yii CModel中rules驗證 擷取錯誤資訊,yiicmodel


在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();//擷取所有驗證欄位的錯誤資訊$model->getErrors('Email');//擷取當前傳入欄位的錯誤資訊$model->getError('Email');// 'message資訊'


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); ?>

這部分來自視圖裡面錯誤輸出部分!
 

http://www.bkjia.com/PHPjc/863231.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/863231.htmlTechArticleYii CModel中rules驗證 擷取錯誤資訊,yiicmodel 在model中定義 public function rules(){ return array ( array ('Name,Email','required'), array ('Email','unique','message'...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.