Codeigniter - 表单验证运行,但不会在 FALSE 上显示错误

我遇到了表单验证的情况。对于如此简单的事情,我似乎无法弄清楚我的问题出在哪里,并且我在网上搜索了很长时间。如果返回 FALSE ,我的表单留空时应该form_error为每个字段抛出错误form_validation。但是,没有任何反应。当表单完全填写时,它会很好地插入到数据库中。我被困住了,我似乎无法弄清楚解决方案是什么。我的想法是代码在某处有拼写错误或表单设计不正确。


这是我的控制器:


function create_job()

    {

        // validate each form field

        $this->form_validation->set_rules('repair_order', 'Repair Order', 'required');

        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');

        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');

        $this->form_validation->set_rules('phone', 'Phone', 'required');

        $this->form_validation->set_rules('vin', 'VIN', 'trim|required');

        $this->form_validation->set_rules('year', 'Vehicle Year', 'trim|required|min_length[4]');

        $this->form_validation->set_rules('make', 'Vehicle Make', 'required');

        $this->form_validation->set_rules('model', 'Vehicle Model', 'required');

        $this->form_validation->set_rules('start_date', 'Start Date', 'required');

        $this->form_validation->set_rules('promise_date', 'Promise Date', 'required');

        $this->form_validation->set_rules('body_hours', 'Body Labor Hours', 'required');

        $this->form_validation->set_rules('paint_hours', 'Paint Labor Hours', 'required');

        $this->form_validation->set_rules('body_tech', 'Body Technician', 'required');

        $this->form_validation->set_rules('paint_tech', 'Paint Technician', 'required');

                );

                $this->db->insert('jobs', $data);


                $this->session->set_flashdata("success", "New job has been successfully created.");

                redirect('main');

            }

            else

            {

                redirect('jobs/new');

            }

    }

我是 CodeIgniter 的新手,只是想学习。如果有人能看到问题出在哪里并能指导我找到答案,我将不胜感激。



人到中年有点甜
浏览 68回答 1
1回答

12345678_0001

您的重定向不会传递 form_error 消息。您需要加载一个视图(您的表单)才能调用 form_error 使用$this->load->view()尝试这个:function create_job(){    // validate each form field    $this->form_validation->set_rules('repair_order', 'Repair Order', 'required');    $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');    $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');    $this->form_validation->set_rules('phone', 'Phone', 'required');    $this->form_validation->set_rules('vin', 'VIN', 'trim|required');    $this->form_validation->set_rules('year', 'Vehicle Year', 'trim|required|min_length[4]');    $this->form_validation->set_rules('make', 'Vehicle Make', 'required');    $this->form_validation->set_rules('model', 'Vehicle Model', 'required');    $this->form_validation->set_rules('start_date', 'Start Date', 'required');    $this->form_validation->set_rules('promise_date', 'Promise Date', 'required');    $this->form_validation->set_rules('body_hours', 'Body Labor Hours', 'required');    $this->form_validation->set_rules('paint_hours', 'Paint Labor Hours', 'required');    $this->form_validation->set_rules('body_tech', 'Body Technician', 'required');    $this->form_validation->set_rules('paint_tech', 'Paint Technician', 'required');        if($this->form_validation->run())        {            $data = array(            'repair_order'      => $this->input->post('repair_order'),            'first_name'        => $this->input->post('first_name'),            'last_name'         => $this->input->post('last_name'),            'address'           => $this->input->post('address'),            'city'              => $this->input->post('city'),            'state'             => $this->input->post('state'),            'zip_code'          => $this->input->post('zip_code'),            'phone'             => $this->input->post('phone'),            'vin'               => $this->input->post('vin'),            'year'              => $this->input->post('year'),            'make'              => $this->input->post('make'),            'model'             => $this->input->post('model'),            'start_date'        => $this->input->post('start_date'),            'promise_date'      => $this->input->post('promise_date'),            'body_hours'        => $this->input->post('body_hours'),            'paint_hours'       => $this->input->post('paint_hours'),            'insurance'         => $this->input->post('insurance'),            'body_tech'         => $this->input->post('body_tech'),            'paint_tech'        => $this->input->post('paint_tech')            );            $this->db->insert('jobs', $data);            $this->session->set_flashdata("success", "New job has been successfully created.");            redirect('main');        }        else        {            $this->load->view('yournewjobform');        }}
打开App,查看更多内容
随时随地看视频慕课网APP