php CodeIgniter 表单验证 valid_email 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9132550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:13:51  来源:igfitidea点击:

CodeIgniter form validation valid_email not working

phpcodeigniter

提问by James Jeffery

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

我正在尝试使用 CodeIgniter 的表单验证类。我使用了“valid_email”参数,从下面的代码可以看出,但即使输入了无效的电子邮件地址,它仍然可以通过验证检查。我用字符串进行了测试: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?

任何人都知道我做错了什么,或者这是否是 CodeIgniter 问题?

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

请注意,我正在设置 flashdata 会话,而不是使用:

<?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).

...这是因为我正在重定向回主页(这是登录页面,因为它是一个私人站点)。

回答by Valeh Hajiyev

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

回答by sdasdadas

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

来自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.