1

I have a form which accepts some text input fields and a file field (used to upload image).

My problem is like that, if I did not fill one of the required fields and I have selected a valid image file, I will get an error message about the missing field, but the image will be uploaded. The Controller is:

    defined('BASEPATH') OR exit('No direct script access allowed');

    class Manage extends MY_Controller
    {
        public function __construct()
        {
            parent::__construct();
        }

        public function index()
        {
            $config = array(
                array(
                    'field' => 'item_name',
                    'label' => 'Item name',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'item_code',
                    'label' => 'Item code',
                    'rules' => 'required|is_unique[_items.item_code]'
                ),
                array(
                    'field' => 'item_description',
                    'label' => 'Item description',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'item_img',
                    'label' => 'Item image',
                    'rules' => 'callback_upload_image'
                )
            );

            $this->form_validation->set_rules($config);
            $this->form_validation->set_message('is_unique', 'Item code (' . $this->input->post('item_code') . ') already exists.');

            if($this->form_validation->run() == FALSE)
            {
                // Render the entry form again
            }
            else
            {
                // Go to another page
            }
        }

        public function upload_image()
        {
            $config['upload_path'] = './items_images';
            $config['max_size'] = 1024 * 10;
            $config['allowed_types'] = 'gif|png|jpg|jpeg';
            $config['encrypt_name'] = TRUE;

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

            if(isset($_FILES['item_img']) && !empty($_FILES['item_img']['name']))
            {
                if($this->upload->do_upload('item_img'))
                {
                    $upload_data = $this->upload->data();
                    $_POST['item_img'] = $upload_data['file_name'];
                    return TRUE;
                }
                else
                {
                    $this->form_validation->set_message('upload_image', $this->upload->display_errors());
                    return FALSE;
                }
            }
            else
            {
                $_POST['item_img'] = NULL;
                return FALSE;
            }
        }
    }

As I know, if one rule failed, other fields and operations will be canceled and the form will be loaded again, or other operations will be done.

Thank you,,,

7
  • ok what is your problem now?what do you want ? what error you are getting Commented Jan 9, 2016 at 8:22
  • If I did not fill one of the required fields and I have selected a valid image file, I will get an error message about the missing field, but the image will still be uploaded. Commented Jan 9, 2016 at 8:24
  • If by required field you mean form field with HTML 5 required attribute then form will not submit. So image will not get uploaded . Commented Jan 9, 2016 at 8:31
  • the required condition has been set using form_validation rules in CodeIgniter not from HTML attribute. Commented Jan 9, 2016 at 8:33
  • so image should not get upload if validation is false? Commented Jan 9, 2016 at 9:13

2 Answers 2

1

you need to upload the image when validation is true only. so remove your public function upload_image() and write the functionalists inside validation true statement as given below.

 defined('BASEPATH') OR exit('No direct script access allowed');
class Manage extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        $config = array(
            array(
                'field' => 'item_name',
                'label' => 'Item name',
                'rules' => 'required'
            ),
            array(
                'field' => 'item_code',
                'label' => 'Item code',
                'rules' => 'required|is_unique[_items.item_code]'
            ),
            array(
                'field' => 'item_description',
                'label' => 'Item description',
                'rules' => 'required'
            ),
            array(
                'field' => 'item_img',
                'label' => 'Item image',
                'rules' => 'callback_upload_image'
            )
        );

        $this->form_validation->set_rules($config);
        $this->form_validation->set_message('is_unique', 'Item code (' . $this->input->post('item_code') . ') already exists.');

        if($this->form_validation->run() == FALSE)
        {
            // Render the entry form again
        }
        else
        {
           $config['upload_path'] = './items_images';
        $config['max_size'] = 1024 * 10;
        $config['allowed_types'] = 'gif|png|jpg|jpeg';
        $config['encrypt_name'] = TRUE;

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

        if(isset($_FILES['item_img']) && !empty($_FILES['item_img']['name']))
        {
            if($this->upload->do_upload('item_img'))
            {
                $upload_data = $this->upload->data();
                $_POST['item_img'] = $upload_data['file_name'];
                return TRUE;
            }
            else
            {
                $this->form_validation->set_message('upload_image', $this->upload->display_errors());
                return FALSE;
            }
        }
        else
        {
            $_POST['item_img'] = NULL;
            return FALSE;
        }
    }
  // Go to another page
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

0

I found this very helpful post here. The author wrote his own rules to validate the file chosen by user to upload. Code sample:

$this->form_validation->set_rules('file', '', 'callback_file_check');


public function file_check($str){
    $allowed_mime_type_arr = array('application/pdf','image/gif','image/jpeg','image/pjpeg','image/png','image/x-png');
    $mime = get_mime_by_extension($_FILES['file']['name']);
    if(isset($_FILES['file']['name']) && $_FILES['file']['name']!=""){
        if(in_array($mime, $allowed_mime_type_arr)){
            return true;
        }else{
            $this->form_validation->set_message('file_check', 'Please select only pdf/gif/jpg/png file.');
            return false;
        }
    }else{
        $this->form_validation->set_message('file_check', 'Please choose a file to upload.');
        return false;
    }
}

Comments

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.