我正在尝试使用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
}
}
任何人都知道我做错了什么或者这是CodeIgniter问题吗?
要注意,我正在设置flashdata会话而不是使用:
<?php echo validation_errors(); ?>
...这是因为我正在重定向回主页(这是登录页面,因为它是一个私人网站)。
答案 0 :(得分: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('/');
}
}
答案 1 :(得分: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.