我使用Ion Auth进行身份验证,并且难以返回验证错误。login();
函数按预期返回它们,当运行register();
时,我被正确地重定向,但是没有返回错误。
表单验证:
$this->load->library('form_validation', array(), 'register_form');
$this->register_form->set_rules('first_name', $this->lang->line('create_user_validation_fname_label'), 'required|xss_clean');
$this->register_form->set_rules('last_name', $this->lang->line('create_user_validation_lname_label'), 'required|xss_clean');
$this->register_form->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email');
$this->register_form->set_rules('phone', $this->lang->line('create_user_validation_phone_label'), 'required|xss_clean');
$this->register_form->set_rules('company', $this->lang->line('create_user_validation_company_label'), 'required|xss_clean');
$this->register_form->set_rules('password', $this->lang->line('create_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->register_form->set_rules('password_confirm', $this->lang->line('create_user_validation_password_confirm_label'), 'required');
注册:
if($this->register_form->run() == true){
$username = strtolower($this->input->post('first_name')) . ' ' . strtolower($this->input->post('last_name'));
$email = $this->input->post('email');
$password = $this->input->post('password');
$additional_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone'),
);
if($this->ion_auth->register($username, $password, $email, $additional_data)){
$this->ion_auth->set_message_delimiters('','');
$this->session->set_flashdata('register_message', $this->ion_auth->messages());
redirect('home/index', 'refresh');
}else{
$this->ion_auth->set_error_delimiters('','');
$this->session->set_flashdata('register_error', $this->ion_auth->errors());
redirect('home/index', 'refresh');
}
}
这就是我如何将flash数据传递给我的视图:
$this->data['register_message'] = $this->session->flashdata('register_message');
$this->data['register_error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('register_error');
在我看来,当与$register_error
相呼应时,我没有得到任何东西,当运行register();
后用print_r($this->session->all_userdata());
打印会话对象时,user_data
数组为空。我正在运行register();
与空字段,所以应该有错误。有什么建议吗?
CodeIgniter "flashdata",将只适用于下一个服务器请求,然后自动清除。
这就是为什么你不能在会话数据中找到它。你可以通过附加的keep_flashdata请求来保存flashdata变量。
$this->session->keep_flashdata('item');
这一行有一个右括号错误。
$this->data['register_error'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('register_error');
改为
$this->data['register_error'] = (validation_errors() ? validation_errors() : $this->session->flashdata('register_error'));