最新消息: 新版网站上线了!!!

yii 自定义rules错误信息

      不要说不懂英文,看下面的代码就行了
public function rules()
{
	return array(
		array('message_id, subject, modified_at', 'required'),
		...
		array('address', 'addressNotInUseByUser', 'on'=>'insert'),
		...
	);
}

Note the $ symbol must be used on both $this-> and $attribute public function.

  // for validating inserts
  public function addressNotInUseByUser($attribute) {
   if (!Yii::app()->user->isGuest and Elist::model()->exists('address=:address and owner_id=:owner_id',array(':address'=>$this->$attribute,':owner_id'=>Yii::app()->user->id)))
      $this->addError($attribute, 'Sorry, you\'re already using that name for another list.');
  }
  // for validating updates
  // called from Controller::Update() action before save as shown below
  public function addressAlreadyInUse($address,$list_id,$owner_id) {
     if (Elist::model()->active()->countByAttributes(array('address'=>$address,'owner_id'=>$owner_id),'id<>'.$list_id)>0) {
     return true;
    } else
    return false;
}

Here’s code in the controller during the Update action that calls the validation rule with paramaters before saving the changed record:

if(isset($_POST['Elist']))
		{
			$model->attributes=$_POST['Elist'];			
	    $model->full_address=$model->address.'@easylist.io';   
	    if ($model->prefix=='') $model->prefix = $model->address;   	    
      if ($model->addressAlreadyInUse($model->address,$model->id,$model->owner_id)) {
  	    Yii::app()->user->setFlash('update_duplicate','Sorry, you already have a list with that name.');   
				$this->redirect(array('update','id'=>$model->id));
      }			
			if($model->save())
				$this->redirect(array('view','id'=>$model->id));
		}

转载请注明:谷谷点程序 » yii 自定义rules错误信息