为什么我的Form_validation总是返回FALSE?

时间:2016-04-20 08:39:31

标签: php codeigniter

您好我试图弄清楚我的代码是什么问题,表单验证总是返回false。 这是我的控制器的代码片段:

function edit() 
{
    //load form_validation class
    $this->load->library('form_validation');

    //set validation rules
    $this->form_validation->set_rules('username','Username','required|trim|xss_clean');
    $this->form_validation->set_rules('password_old','password lama','required|trim|xss_clean');
    $this->form_validation->set_rules('password_new1','password baru','required|trim|xss_clean');
    $this->form_validation->set_rules('password_new2','confirm password','required|trim|xss_clean|matches[password_new1]');

    if ($this->form_validation->run() == FALSE)
    {
        $this->session->set_flashdata('flash_data', 'Lengkapi Form Dengan Benar dan Lengkap');
        redirect('admin/akun');
    }
    else
    {
        $this->load->model("user_model","user");
        if($_POST)
        {
            $result = $this->user->edit_user($_POST);
            if($result == FALSE)
            {
                $data['page'] = $this->config->item('ci_my_admin_template_dir_admin') . "error_message";
                $this->load->view($this->_container, $data);
            }
            else
            {
                $this->session->set_flashdata('flash_data', 'Silahkan Log In dengan Username dan Password yang baru!');
                redirect('admin/logout');
            }
        }
    }
}                   

是否有任何其他配置我应该更改以使其工作??

感谢您的帮助..

编辑: 求助 - >我的错误我没有在我的验证规则中添加xss_clean而加载助手(' security')

1 个答案:

答案 0 :(得分:0)

您必须使用Codeigniter Input类。

    function edit() 
    {
        //load form_validation class
        $this->load->library('form_validation');

        //set validation rules
        $this->form_validation->set_rules('username','Username','required|trim|xss_clean');
        $this->form_validation->set_rules('password_old','password lama','required|trim|xss_clean');
        $this->form_validation->set_rules('password_new1','password baru','required|trim|xss_clean');
        $this->form_validation->set_rules('password_new2','confirm password','required|trim|xss_clean|matches[password_new1]');

        if ($this->form_validation->run() == FALSE)
        {
            $this->session->set_flashdata('flash_data', 'Lengkapi Form Dengan Benar dan Lengkap');
            redirect('admin/akun');
        }
        else
        {
           $data['password_old'] = $this->input->post('password_old');
           $data['password_new1'] = $this->input->post('password_new1');
           $data['password_new1'] = $this->input->post('password_new1');

            $this->load->model("user_model","user");

                $result = $this->user->edit_user($data);
                if($result == FALSE)
                {
                    $this->session->set_flashdata('flash_data', 'Silahkan Log In dengan Username dan Password yang baru!');
                    redirect('admin/logout');
                }
                else
                {
                    $data['page'] = $this->config->item('ci_my_admin_template_dir_admin') . "error_message";
                    $this->load->view($this->_container, $data);

                }

        }
    }   
}