2012-07-31 97 views
3

我想使用規則驗證失敗錯誤消息顯示自定義錯誤消息。我該怎麼做?yii如何使用驗證失敗顯示自定義錯誤消息msg

從控制器

$expire_date_error = '<p>Please enter the company license expire date more than notification days</p> 
        <ul> 
        <li>Expire Date is less than notificaion days on current date.</li> 
        </ul>'; 
       Yii::app()->user->setFlash('expire_date_error',$expire_date_error); 

     $this->render('create',array(
      'model'=>$model, 

從這個觀點得到這個味精味精集。

<?php if(Yii::app()->user->hasFlash('expire_date_error')):?> 
     <div class="errorMessage"> 
      <?php echo Yii::app()->user->getFlash('expire_date_error'); ?> 
     </div> 
    <?php endif; ?> 

我使用了一些代碼http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/

問候 ======================= ++++++++ +++++++++++++++++++++++++ ========================= =

更新問題。

這是我的控制器

public function actionCreate() 
    { 
     $model=new CompanyLicense; 

     // Uncomment the following line if AJAX validation is needed 
     // $this->performAjaxValidation($model); 

     if(isset($_POST['CompanyLicense'])) 
     { 
      $currentTime = date('Y-m-d h:i:s', time());  

      $model->attributes= $_POST['CompanyLicense']; 
      $model->created = $currentTime; 
      $model->active = 1; 

      $model->expire_date= $_POST['CompanyLicense']['expire_date']; 

      if($model->checkExpireDate()) 
      { 
       $model->file_name=CUploadedFile::getInstance($model,'file_name'); 
       $folder = Yii::getPathOfAlias('webroot').'/images/'; 
       if($model->save()) 
       { 
        mkdir($folder.$model->id); 
        $model->file_name->saveAs($folder. $model->id . "/" . $model->file_name); 
        $this->redirect(array('view','id'=>$model->id)); 
       } 
      }else{ 
       //echo 'debug'; 
       $expire_date_error = '<p>Please enter the company license expire date more than notification days</p> 
        <ul> 
        <li>Expire Date is less than notificaion days on current date.</li> 
        </ul>'; 
       Yii::app()->user->setFlash('expire_date_error',$expire_date_error); 
      } 
     } 

     $this->render('create',array(
      'model'=>$model, 
     )); 
    } 
public function checkExpireDate($attribute='',$params ='') 
    { 
     $currentTime = date('Y-m-d', time()); 
     $currentTime = date('Y-m-d', strtotime($currentTime . '+ ' . $this->notification_days.' days')); 
     if ($currentTime > $this->expire_date) 
      return false; 

     return true; 
      //$this->addError($this->expire_date,'should be more ' . $this->notification_days . ' days on current date!'); 
    } 

我想說明這個錯誤與其他驗證失敗錯誤味精。

回答

0

請閱讀本

http://www.yiiframework.com/wiki/1/how-to-customize-the-error-message-of-a-validation-rule/

或嘗試

class Post extends CActiveRecord 
{ 
    public function rules() 
    { 
     return array(
      array('title, content', 'required', 
        'message'=>'Please enter a value for {attribute}.'), 
      // ... other rules 
     ); 
    } 
} 

在上文中,所述定製錯誤消息包含一個預定義的佔位符{屬性}。 CRequiredValidator(其別名是必需的)將使用未通過驗證的實際屬性名稱替換此佔位符。

+0

THX您的快速回復。但這不是我想要的。我有2個字段,通知日期和過期日期。如果(過期日期<(當前日期+通知日期)),我的自定義錯誤消息將顯示。否則,不顯示。 – 2012-07-31 04:24:29

0

如果使用自定義驗證函數,則:

class Post extends CActiveRecord 
{ 
    public function rules() 
    { 
     return array(
      array('expire_date_error', 'check'), 
     ); 
    } 

    public function check() 
    { 
     if($this->a > $this->b) { 
      $this->addError('expire_date_error', 'New custom error message'); 
      return false; 
     } 

     return true; 
    } 
} 
+0

科羅托夫斯克,謝謝你的回覆。對不起,你不是我想要的。我用我的控制器更新了我的問題。請看看並幫助我。 :) – 2012-07-31 08:47:42

+0

好的,只需設置字符串「過期日期小於當天的通知天數」。到'$ this-> addError()'並驗證屬性'expire_date',例如函數check()。最後你的檢查方法應該是:http://paste.ubuntu.com/1121289/ – 2012-07-31 12:03:17

+0

@tharsoe,控制器:http://paste.ubuntu.com/1121292/ 你可以通過$ model->來查看驗證錯誤, getErrors()並做出任何佈局,你想要什麼 – 2012-07-31 12:07:04

相關問題