2012-02-03 140 views
3

我想使用CodeIgniter的表單驗證類。從以下代碼可以看出,我使用了「valid_email」參數,但即使輸入了無效的電子郵件地址,它仍然通過驗證檢查。我以字符串測試:是testing123CodeIgniter表單驗證valid_email不工作

public function login() 
{ 
    $this->form_validation->set_rules('authEmail', 'trim|required|valid_email|xss_clean'); 
    $this->form_validation->set_rules('authPassword', 'trim|required'); 

    $email = $this->input->post('authEmail'); 
    $password = $this->input->post('authPassword'); 

    if($this->form_validation->run() === FALSE) { 
     $this->session->set_flashdata('formValidationError', validation_errors('<p class="error">', '</p>')); 
     redirect('/'); 
    } else { 
     // Begin authentication 
    } 
} 

任何人有任何想法,我做錯了,或者如果這是一個笨問題?

要注意,我設置一個flashdata會話,而不是使用:

<?php echo validation_errors(); ?> 

...這是因爲我在做一個重定向到主頁(這是登錄頁面,因爲它是一個私人現場)。

回答

6

試試這個:

public function login() 
{ 
    $this->load->library('form_validation'); 

    $this->form_validation->set_rules('authEmail', 'Email', 'trim|required|valid_email|xss_clean'); 
    $this->form_validation->set_rules('authPassword', 'Password', 'trim|required'); 

    if($this->form_validation->run() !== false){ 
    //validation passed 
    $email = $this->input->post('authEmail'); 
    $password = $this->input->post('authPassword'); 
    // Begin authentication 
    } 
    else { 
     $this->session->set_flashdata('formValidationError', validation_errors('<p class="error">', '</p>')); 
     redirect('/'); 
    } 
} 
+2

我不明白是什麼的問題,你的答案不同的驗證規則? – KTAnj 2015-06-20 03:59:13

1

我只是學習使用它,但是你不需要三個參數嗎?這是從他們的表單驗證頁面:

$this->form_validation->set_rules('email', 'Email', 'required'); 

http://codeigniter.com/user_guide/libraries/form_validation.html#validationrules

The field name - the exact name you've given the form field. 

A "human" name for this field, which will be inserted into the error message. For example, if your field is named "user" you might give it a human name of "Username". Note: If you would like the field name to be stored in a language file, please see Translating Field Names. 

The validation rules for this form field.