0

每次我更新我的设置并将输入 type="file" 留空。$this->form_validation->run() 总是返回 false。我什至没有使用 setrules 并且需要它。

这是我的代码:

public function update_settings()
{
    $this->load->model('bus_owner_model');

    $data = $this->bus_owner_model->get_business_data_id_by_row($this->session->userdata('id'));

    $this->load->library('form_validation');

    $this->form_validation->set_rules('business_name', 'Business Name', 'required|trim|xss_clean');
    $this->form_validation->set_rules('type_of_business', 'Type of Business', 'required|trim');
    $this->form_validation->set_rules('country', 'Country', 'required|trim');
    $this->form_validation->set_rules('address', 'Address', 'required|trim');
    $this->form_validation->set_rules('city', 'City', 'required|trim|alpha');
    $this->form_validation->set_rules('email_address', 'Email Address', 'required|trim|valid_email');
    $this->form_validation->set_rules('contact', 'Contact', 'required|trim|numeric|min_length[7]');
    $this->form_validation->set_message('is_unique', 'Your email address is already been taken');

    if($this->form_validation->run() == true)
    {
        $config['upload_path']      = "./_profile_images/";
        $config['allowed_types']    = 'jpg|jpeg|gif|png';
        $config['encrypt_name']     = "true";
        $this->load->library('upload', $config);    

        if(!$this->upload->do_upload())
        {

            $this->bus_settings($this->upload->display_errors());
        }else
        {
            $this->load->model('bus_owner_model');

            $file_data = $this->upload->data();

            $data = array(
                'company_name'      => $this->input->post('business_name'),
                'profile_img'       => $file_data['file_name'],
                'type_description'  => $this->input->post('type_of_business'),
                'country'           => $this->input->post('country'),
                'address'           => $this->input->post('address'),
                'state'             => $this->input->post('state'),
                'city'              => $this->input->post('city'),
                'email_address_c'   => $this->input->post('email_address'),
                'contact'           => $this->input->post('contact')
            );

            if($this->bus_owner_model->update_bus($data) == true)
            {
                redirect('bus_owner/bus_settings');
            }else
            {
                $this->load->model('bus_owner_model');
                $this->load->model('user_model');
                $data['latlng']                                     = $this->bus_owner_model->get_latlng();
                $data['get_all_business_data_orderby_total_result'] = $this->bus_owner_model->get_all_business_data_orderby_total();
                $data['get_all_user_data_orderby_total_result']     = $this->user_model->get_all_user_data_orderby_total();
                $data['result_countries']                           = $this->bus_owner_model->get_countries();
                $data['get_business_data_result']                   = $this->bus_owner_model->get_business_data();

                $data = array('error' => 'unable to upload files');
                $this->load->view('site_header', $data);
                $this->load->view('bus_owner_views/left_col_map_single');
                $this->load->view('left_col_static', $data);
                $this->load->view('bus_owner_views/right_col_settings', $data);
                $this->load->view('site_footer');               
            }           
        }                       
    }
}

然后它会返回到它的页面并且什么都没有发生。

4

1 回答 1

0

I think you are trying to upload file and apply normal field validation on it.That's the wrong way you are trying to validate.Because file field have different behaviour than normal input field.Try this way only for file field.

public function update_settings()
{
   $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

             redirect('bus_owner/bus_settings');
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
}.

For more details goto link.

于 2013-04-05T04:01:29.490 回答