Codeigniter:表单验证is_unique错误未显示

时间:2012-06-12 02:58:46

标签: php codeigniter validation

我使用以下行检查我的添加用户表单,以检查我的数据库中是否存在该电子邮件地址我觉得它工作正常,但我无法收到任何错误消息。

$this->form_validation->set_rules('userEmail','E-Mail', 'required|valid_email|trim|max_length[99]|xss_clean|is_unique[users.email]');

在我看来,我有<?php echo validation_errors(); ?>

我的控制器格式如下:

if($this->form_validation->run() === TRUE)
        {
            $userData = array(
                'fName' => $this->input->post('userFirstName', TRUE),
                'lName' => $this->input->post('userLastName', TRUE),
                'email' => $this->input->post('userEmail', TRUE),
                'password' => sha1($this->input->post('userPassword', TRUE))
            );  

            $this->db->escape($userData);
            $this->user_model->addUser($userData);
        }

3 个答案:

答案 0 :(得分:1)

我将控制器更改为以下内容:

$this->form_validation->set_rules('userEmail','E-Mail', 'required|valid_email|trim|max_length[99]|xss_clean|is_unique[users.email]');
        $this->form_validation->set_rules('userPassword','Password', 'required|trim|max_length[99]|xss_clean');

        if($this->form_validation->run() === TRUE)
            {
                $userData = array(
                    'fName' => $this->input->post('userFirstName', TRUE),
                    'lName' => $this->input->post('userLastName', TRUE),
                    'email' => $this->input->post('userEmail', TRUE),
                    'password' => sha1($this->input->post('userPassword', TRUE))
                );  

                $this->db->escape($userData);
                $this->user_model->addUser($userData);
            }

            $data['contentMangement'] = $this->options_model->systemOptions();
            $data['pageTitle'] = 'Add User';
            $this->load->view('_assets/header', $data);
            $this->load->view('addUser', $data);
            $this->load->view('_assets/footer');
    }

答案 1 :(得分:0)

只需使用is_unique预定义验证规则即可。传递电子邮件列的表名和列名。

$this->form_validation->set_rules('userEmail','E-Mail', 'required|valid_email|trim|max_length[99]|xss_clean|is_unique[users.email]');

答案 2 :(得分:0)

我们必须为 is_unique 添加表名 在我的方式为exp。

array(
            'field' => 'email',
            'label' => 'email',
            'rules' => 'trim|required|valid_email|is_unique[users.email]',
            'errors' => array('required'  => 'Please enter a %s.','valid_email' => 'Please enter a valid %s.','is_unique' => 'email already exist.')
        ),
希望你站稳脚跟。