Form validation with optional File Upload field callback

Posted by MotiveKyle on Stack Overflow See other posts from Stack Overflow or by MotiveKyle
Published on 2012-12-05T16:30:15Z Indexed on 2012/12/05 17:04 UTC
Read the original article Hit count: 222

Filed under:
|

I have a form with some input fields and a file upload field in the same form. I am trying to include a callback into the form validation to check for file upload errors.

Here is the controller for adding and the callback:

public function add() {

    if ($this->ion_auth->logged_in()):

        //validate form input
        $this->form_validation->set_rules('title', 'title', 'trim|required|max_length[66]|min_length[2]');
        // link url
        $this->form_validation->set_rules('link', 'link', 'trim|required|max_length[255]|min_length[2]');
        // optional content
        $this->form_validation->set_rules('content', 'content', 'trim|min_length[2]');

        $this->form_validation->set_rules('userfile', 'image', 'callback_validate_upload');

        $this->form_validation->set_error_delimiters('<small class="error">', '</small>');

        // if form was submitted, process form
        if ($this->form_validation->run())
        {
            // add pin
            $pin_id = $this->pin_model->create();
            $slug = strtolower(url_title($this->input->post('title'), TRUE));

                // path to pin folder
                $file_path = './uploads/' . $pin_id . '/';

                // if folder doesn't exist, create it
                if (!is_dir($file_path)) {
                    mkdir($file_path);
                }

                // file upload config variables
                $config['upload_path'] = $file_path;
                $config['allowed_types'] = 'jpg|png';
                $config['max_size'] = '2048';
                $config['max_width']  = '1920';
                $config['max_height']  = '1080';
                $config['encrypt_name'] = TRUE;

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

                // upload image file
                if ($this->upload->do_upload()) {

                    $this->load->model('file_model');
                    $image_id = $this->file_model->insert_image_to_db($pin_id);
                    $this->file_model->add_image_id_to_pin($pin_id, $image_id);

                }

        }

        // build page

    else:

        // User not logged in
        redirect("login", 'refresh');

    endif;
}

The callback:

function validate_upload() {
    if ($_FILES AND $_FILES['userfile']['name']):
            if ($this->upload->do_upload()):
                return true;
            else:
                $this->form_validation->set_message('validate_upload', $this->upload->display_errors());
                return false;
            endif;
    else:
        return true;
    endif;
}

I am getting the error Fatal error: Call to a member function do_upload() on a non-object on line 92 when I try to run this.

Line 92 is the if ($this->upload->do_upload()): line in the validate_upload callback.

Am I going about this the right way? What's triggering this error?

© Stack Overflow or respective owner

Related posts about php

Related posts about codeigniter