當我的應用程序用戶註冊下列驗證規則提交前稱爲:不叫上驗證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並搜索論壇,但無法理解爲什麼我的自定義驗證方法未被調用?
你在使用檢查模型中的錯誤? '$模型 - > getErrors()'? 該模型的場景是什麼? 你有一個自定義'beforeValidate()'方法嗎?如果是這樣,你是否真的回來了? 您是否嘗試將'CApplication :: end();'放入該驗證方法中以查看它是否真的被調用? – Ben 2012-02-09 01:12:24