Welcome Guest, Not a member yet? Register   Sign In
How to validate a file upload with the upload class in a multipart form?
#1

[eluser]bgougent[/eluser]
I have this function
Code:
function add()
    {
        $config['upload_path'] = '/Path to upload/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '1024';
        $this->upload->initialize($config);

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('code', 'Code', 'required');
        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('description', 'Description', '');
        if (!$this->upload->do_upload())
        {
            $this->form_validation->set_rules('userfile','Picture','required');
        }

        if ($this->form_validation->run() == FALSE)
        {
            $content['form'] = 'default multipart form';
            
                        $this->load->view('template',$content);
        }
        else
        {
            $this->add_data();
        }
    }

where I've put
Code:
if (!$this->upload->do_upload())
        {
            $this->form_validation->set_rules('userfile','Picture','required');
        }
as validation for my upload. Is this the correct way. It is working. But personal I think there must be a better integration of the upload class with the form validation.
#2

[eluser]skunkbad[/eluser]
Take a look at the upload class, and its methods, and you will see that you don't need to use the form validation class at all (at least for images). If there was some reason to do further validation of an uploaded file, you would extend the upload class. The form validation class validates strings, integers, and boolean values, and is not really suitable for file validation.
#3

[eluser]bgougent[/eluser]
You're correct. But then I need to validate before the form or after the form validation. It would be nice just to have the upload class integrated in the form.

It seems that the upload class is more written to use as a stand alone form and not to mix with a default html form. Since I use the upload class my post value of the input-file is empty.
#4

[eluser]skunkbad[/eluser]
I just did this exact same thing for something I'm working on. I have a file upload mixed with 2 standard form input values. If you'd like to see what I did, check out the new inventory controller / add_category method of my Community Cart application, located on Bitbucket:

http://bitbucket.org/skunkbad/community-cart

While there is probably a more elegant solution, it accomplishes what I needed, and displays the appropriate error messages and confirmation messages.
#5

[eluser]ontguy[/eluser]
This is how I done it. Mainly based on inspiration from others.

controller
Code:
function add()
{
  $this->_load_product_validation();

  if ($this->form_validation->run() === FALSE)
  {
    $this->messages->add(validation_errors(), 'error');
    $this->loadview('admin/product/add', $this->data);
  }
  ........
}  

function _load_product_validation()
{
  $this->load->library('form_validation');

  //actually upload done in MY_Form_Validation
  $this->form_validation->set_rules('file_validate', 'file upload', 'file_upload_check[FALSE]');

      $this->form_validation->set_error_delimiters('', '<br />');
}

view
Code:
&lt;?php echo form_open_multipart('admin/product/add'); ?&gt;
    &lt;input type="hidden" name="file_validate" value="upload_check"&gt;
    &lt;input type="file" name="image_filename" size="20" /&gt;
    &lt;input type="submit" value="Add Product" /&gt;
    &lt;input type="reset" /&gt;
&lt;?php echo form_close(); ?&gt;

MY_Form_validation
Code:
function file_upload_check($file, $required = 'FALSE')
{
  if ($_FILES['image_filename']['error'] == 4 && $required == 'FALSE')
  {
    return TRUE;
  }

  if ( ! $this->CI->catalog->upload_file('image_filename'))
  {
    $this->CI->form_validation->set_message('file_upload_check', $this->CI->upload->display_errors('', ''));
    return FALSE;
  }
  else
  {
    return TRUE;
  }
}

in a library
Code:
function upload_file($field = null)
{
      $config['upload_path'] = $this->config->item('upload_path');
      $config['allowed_types'] = 'gif|jpg|png|jpeg';
      $config['max_size']    = '1024';
      $config['max_width'] = '1024';
      $config['max_height'] = '768';
      $config['overwrite'] = TRUE;
      $config['encrypt_name'] = TRUE;
      $this->load->library('upload');
      $this->upload->initialize($config);

      if ( ! $this->upload->do_upload($field))
      {
        return FALSE;
      }

  $upload_data = $this->upload->data();
  $this->upload_file_name = $upload_data['file_name'];

  return TRUE;
}




Theme © iAndrew 2016 - Forum software by © MyBB