2012-02-08 109 views
2

當我的應用程序用戶註冊下列驗證規則提交前稱爲:不叫上驗證Yii的自定義的驗證規則()

public function rules() 
{ 
    return array(
     array('email, firstName, lastName, password, passwordConfirm, telephone', 'required'), 
     array('club, email, firstName, lastName, level, password, telephone', 'length', 'max'=>45), 
     array('passwordConfirm', 'compare', 'compareAttribute'=>'password', 'on'=>'register'), 
     array('email', 'isUniqueEmailAddress'), 
    ); 
} 

前三驗證規則成功完成,但第四(自定義驗證方法)不。應該調用函數「isUniqueEmailAddress」並執行以下操作:

/* 
* Returns true if there doesn't exist a user in the database with the submitted email 
*/ 
public function isUniqueEmailAddress($attribute, $params) 
{ 
    //if (User::model()->find('email=:email', array(':email'=>$this->email)) !== null) 
    //{ 
     $this->addError('email', 'Email account already exists'); 
    //} 
} 

正如你所看到的,我甚至註釋掉所有的邏輯簡單地確保驗證錯誤被髮回登記表,但沒有驗證()返回錯誤。我已閱讀Yii's Documentation並搜索論壇,但無法理解爲什麼我的自定義驗證方法未被調用?

+3

你在使用檢查模型中的錯誤? '$模型 - > getErrors()'? 該模型的場景是什麼? 你有一個自定義'beforeValidate()'方法嗎?如果是這樣,你是否真的回來了? 您是否嘗試將'CApplication :: end();'放入該驗證方法中以查看它是否真的被調用? – Ben 2012-02-09 01:12:24

回答

0

我評論了一些可能的原因,您的代碼不工作(它看起來是正確的),但在這種情況下,您可以使用Unique驗證程序。

array('email', 'unique')會做到這一點。

1

我試圖在調用$ model-> save()之後在控制器中設置一個flash消息時遇到了這個問題。這是導致邏輯錯誤的代碼格式問題。

例如,在模型中我有這樣的事情:

public function validateNumItems($attribute, $params) { 
    $this->addError($attribute, 'Validate Items Failed'); 
} 

理論上,這應該引起表單提交失敗,每一次進行調試。但它從來沒有。

我的控制器的更新操作是這樣的:

public function actionUpdate($id) { 
    $model = $this->loadModel($id); 

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

    if (isset($_POST['Collection'])) 
    { 
     $model->attributes = $_POST['Collection']; 
     if ($model->save()) 
     Yii::app()->user->setFlash('success', "Data saved!"); 
     $this->redirect(array('index')); 
    } 

    $this->render('update', array('model' => $model,)); 
    } 

各地新聞司創建的$模型 - >保存()測試的缺失括號,當我加入新行引起了邏輯錯誤。把括號放在固定的地方。

public function actionUpdate($id) { 
    $model = $this->loadModel($id); 

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

    if (isset($_POST['Collection'])) { 
     $model->attributes = $_POST['Collection']; 
     if ($model->save()) { 
      Yii::app()->user->setFlash('success', "Data saved!"); 
      $this->redirect(array('index')); 
     } 
    } 

    $this->render('update', array('model' => $model,)); 
    } 
1
array('email', 'exist') 

這樣。

public function rules() 
{ 
    return array(
     array('email, firstName, lastName, password, passwordConfirm, telephone', 'required'), 
     array('club, email, firstName, lastName, level, password, telephone', 'length', 'max'=>45), 
     array('passwordConfirm', 'compare', 'compareAttribute'=>'password', 'on'=>'register'), 
     array('email', 'exist') 
    ); 
} 
0

試試這個,它的正常工作 -

public function rules() 
    { 
     return array(
      array('email, firstName, lastName, password, passwordConfirm, telephone', 'required'), 
     array('email', 'email','message'=>"The email isn't correct"), 
      array('email', 'uniqueEmail'), 
     ); 
    } 

您的自定義功能,寫入同一型號 -

public function uniqueEmail($attribute, $params) 
    { 
     if($user = User::model()->exists('email=:email',array(':email'=>$this->email))) 
      $this->addError($attribute, 'Email already exists!'); 
    }