1

I have set form validation in my project. My all form validation is working but I can not display file input validation error message. The file is upload properly, it doesn't show any error when I upload an invalid file. I try a lot of ways but anythings are not working.

I give here only file uploading related code.

My controller

    $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);

    }

MY model

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

My view

    <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>

How it can be fixed?

7
  • 1
    please explain: "is not working", Do you mean the validation of the file or the file uploading itself, or both?. You need to edit your question to explain better where you have a problem. Commented Apr 21, 2019 at 19:48
  • 1
    While you are answering @Vickel please show any validation rules you have set and what you consider a "valid file input" and where you expect it to be validated. Thanks Commented Apr 21, 2019 at 20:09
  • 1
    @Vickel Now you can see. I explain properly. Thanks for the suggestion. Commented Apr 22, 2019 at 5:07
  • @DFriend bro, have a look, please. Commented Apr 22, 2019 at 5:16
  • 1
    Do you have any other validation rules for other inputs? Commented Apr 22, 2019 at 12:55

1 Answer 1

1
+50

I think you should separate $this->form_validation->run() and $this->upload->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
 }
Sign up to request clarification or add additional context in comments.

6 Comments

Then how I write my view code to display the error message??
It shows me other validation errors but not shows the file error.
Have you add the file validation code in controller like this if (empty($_FILES['userfile']['name'])) { $this->form_validation->set_rules('userfile', 'Document', 'required'); }
Have you used enctype="multipart/form-data" in your form tag?
Yes, it is working, but it shows me all the error together. After seeing this picture you may get a better idea, imgur.com/a/AkLQXFQ. I want to show error like my other forms
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.