【问题标题】:Can't display file validation error message in codeigniter无法在 codeigniter 中显示文件验证错误消息
【发布时间】:2019-04-21 18:43:23
【问题描述】:

我在我的项目中设置了表单验证。我的所有表单验证都在工作,但我无法显示文件输入验证错误消息。该文件已正确上传,当我上传无效文件时它不会显示任何错误。我尝试了很多方法,但没有任何效果。

我这里只给出文件上传相关的代码。

我的控制器

    $config = [
        'upload_path'=>'./uploads/image/',
        'allowed_types'=>'jpg|png',
        'max_size' => '400',
        'overwrite' => FALSE
        ];

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

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

        $view['admin_view'] = "admin/add_books";
        $this->load->view('layouts/admin_layout', $view);

    }

我的模特

public function add_books()
{
    $data = $this->upload->data();
    $image_path = base_url("uploads/image/".$data['raw_name'].$data['file_ext']);

    $data = array(
        'book_name' => $this->input->post('book_name'),
        'description' => $this->input->post('description'),
        'author' => $this->input->post('author'),
        'publisher' => $this->input->post('publisher'),
        'price' => $this->input->post('price'),
        'quantity' => $this->input->post('quantity'),
        'categoryId' => $this->input->post('categoryId'),
        'book_image' => $image_path,
        'userId' => $this->session->userdata('id'),
        'status' => $this->input->post('status')
    );

    $insert_book = $this->db->insert('books', $data);
    return $insert_book;
}

我的看法

    <div class="form-group row">
        <label for="book_image" class="col-sm-2 col-form-label">Book image</label>
        <div class="col-sm-6">
            <?= form_upload(['name'=>'userfile', 'class'=>'form-control'])?>
            <div class="text-secondary">* Upload PNG, JPG format. Image should not be more than 400KB</div>
        </div>
        <div class="col-sm-4">
           <div class="text-danger form-error"><?= form_error('userfile')?></div>    
        </div>
    </div>

如何解决?

【问题讨论】:

  • 请解释:“不工作”,你是指文件的验证或文件上传本身,或两者兼而有之?您需要编辑您的问题,以便更好地解释您的问题所在。
  • 当您回答@Vickel 时,请显示您设置的任何验证规则以及您认为“有效文件输入”的内容以及您希望验证的位置。谢谢
  • @Vickel 现在你可以看到了。我解释得当。感谢您的建议。
  • @DFriend 兄弟,请看一下。
  • 您对其他输入还有其他验证规则吗?

标签: file codeigniter validation


【解决方案1】:

我觉得你应该把$this-&gt;form_validation-&gt;run()$this-&gt;upload-&gt;do_upload()分开

$view = array();
if($this->form_validation->run() == true){

    if(!$this->upload->do_upload('userfile')){
        $view['error'] = $this->upload->display_errors();
    }
 }else{
     $view['error'] = validation_errors();
 }

 if(array_key_exists('error', $view)){
      $view['admin_view'] = "admin/add_books";
      $this->load->view('layouts/admin_layout', $view);
 }else{
     //Insert the record
 }

【讨论】:

  • 那我怎么写我的view code来显示错误信息??
  • 它显示了其他验证错误,但不显示文件错误。
  • 你有没有像这样在控制器中添加文件验证码if (empty($_FILES['userfile']['name'])) { $this-&gt;form_validation-&gt;set_rules('userfile', 'Document', 'required'); }
  • 你在表单标签中使用过enctype="multipart/form-data"吗?
  • 是的,它正在工作,但它向我显示了所有错误。看到这张照片后,你可能会有更好的想法,imgur.com/a/AkLQXFQ。我想像我的其他表单一样显示错误
猜你喜欢
  • 2017-01-25
  • 1970-01-01
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
  • 2018-10-30
  • 1970-01-01
相关资源
最近更新 更多