简体   繁体   中英

CodeIgniter form validation valid_email not working

I am trying to use CodeIgniter's form validation class. I've used the "valid_email" parameter, as can be seen from the code below, but even when an invalid email address is entered it still passes the validation check. I tested with the string: testing123

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
    }
}

Anyone have any idea what I'm doing wrong or if this is a CodeIgniter issue?

To note, I am setting a flashdata session as opposed to using:

<?php echo validation_errors(); ?>

... this is because I am doing a redirect back to the homepage (which is the login page as it's a private site).

Try this:

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('/');
    }
}

I'm just learning to use it as well, but don't you need three parameters? This is from their form validation page:

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

From 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM