2010-11-05 79 views
2

這是我在控制器上註冊一個新用戶的註冊方法。Codeigniter自定義表單驗證

function register() { 

    $config_rules = array(
      array(
       'field' => 'txtEmail', 
       'label' => 'Email', 
       'rules' => 'required|valid_email' 
      ), 
      array(
       'field' => 'txtPassword', 
       'label' => 'Password', 
       'rules' => 'required|min_length[6]' 
      ), 
      array(
       'field' => 'txtRePassword', 
       'label' => 'Re-type Password', 
       'rules' => 'required|min_length[6]' 
      ) 
     ); 
    $this->form_validation->set_rules($config_rules); 

    if(isset($_POST['btnSubmit']) && $this->form_validation->run() == TRUE) 
    { 
     // insert query and redirect to registration success page 
    } 

    $this->load->view('users/register_form'); 
} 

表單驗證的所有規則工作正常。 但是,問題是我不能做的是驗證密碼並重新輸入密碼。

如何進行自定義表單驗證,例如檢查密碼和重新輸入密碼 是否相同。然後返回false進行驗證,並通過validation_errors()告訴錯誤消息,告訴密碼和重新輸入密碼不相同。

回答

2

地址:

matches[txtRePassword] 

密碼規則,那麼你可以使用:

$this->validation->set_message('matches', 'New Passwords don\'t match'); 

,使客戶的錯誤消息