2012-11-30 96 views
1

我使用CodeIgniter 2和DataMapper ORM。 對於用戶來說,我有confirm_passwordconfirm_email場(加上其他人),這兩者都沒有在數據庫中的字段(表users沒有這些字段),但它只是爲了顯示註冊表單上:CodeIgniter 2,DataMapper驗證規則

我也有後端,這兩個字段(confirm_passwordconfirm_email)不存在於表單中。

public $validation = array(
'first_name' => array(
    'label' => 'lang:common_first_name', 
    'rules' => array('required', 'trim') 
), 
'last_name' => array(
    'label' => 'lang:common_last_name', 
    'rules' => array('trim') 
), 
'email' => array(
    'label' => 'lang:common_email', 
    'rules' => array('required', 'trim', 'unique', 'valid_email') 
), 
'confirm_email' => array(
    'label' => 'lang:common_confirm_email', 
    'rules' => array('matches' => 'email') 
), 
'password' => array(
    'label' => 'lang:common_password', 
    'rules' => array('required', 'min_length' => 6, 'encrypt') 
), 
'confirm_password' => array(
    'label' => 'lang:common_confirm_password', 
    'rules' => array('matches' => 'password') 
) 

);

如果我不作confirm_emailconfirm_email領域需要,驗證不會觸發比賽規則。 如果我讓他們要求,那麼後端沒有這些字段,觸發confirm_emailconfirm_password,但它不應該。

  • 包含我們在應用程序中可能具有的所有可能的驗證規則(在課程的模型中)是否最好?
  • 當在 後端添加用戶時,在控制器中更改這些規則(例如,從$validation數組中刪除 confirm_email索引)是否是一個好主意?

我很欣賞任何想法。 謝謝

回答

0

你有最新版本的ORM Mapper,根據文檔你可以添加非數據庫字段。

http://stensi.com/datamapper/pages/validation.html

此外,您現在可以添加驗證規則對非數據庫表字段, 如「確認電子郵件地址」或「確認密碼」。例如:

,你不需要的確認領域中要求規則 - 因爲它們表明,申請在DB需要因此DB錯誤) - 在比賽屬性將做所需的對匹配字段進行驗證(例如,如果匹配不匹配,則會拋出錯誤。)換句話說,您只需要在'email'字段中進行驗證,如果匹配字段不匹配,那麼confirmation_email會拋出錯誤。在空白領域,你真的需要證明電子郵件是必需的。

最後 - 你可以刪除索引,但通常這不是個好主意。相反,如果上述失敗 - 在控制器中添加表單驗證規則。