【问题标题】:Returning false $this->form_validation->run() when there is an empty input type="file"有空输入 type="file" 时返回 false $this->form_validation->run()
【发布时间】:2023-03-09 13:20:01
【问题描述】:

每次我更新我的设置并将输入 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');               
            }           
        }                       
    }
}

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

【问题讨论】:

  • 您是否尝试过将代码缩减到仍然存在问题的最小尺寸?除非您自己付出一些努力,否则 SO 不是“请为我调试代码”网站。

标签: php jquery css html codeigniter


【解决方案1】:

我认为您正在尝试上传文件并对其应用常规字段验证。这是您尝试验证的错误方式。因为文件字段与正常输入字段的行为不同。仅对文件字段尝试这种方式。

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);
        }
}.

更多详情请转至link

【讨论】:

  • 我需要两个表格吗?前任。 (文件上传表格)和更新详细信息的另一个表格?
  • 只有一种形式,以上代码仅用于文件上传。将以上代码添加到您的函数中。但将其用于form_open_multipart()或enctype。
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多