2012-02-03 124 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.