Welcome Guest, Not a member yet? Register   Sign In
Form Validation Library in conjunction with File Upload Library
#1

[eluser]Adam Griffiths[/eluser]
Hey guys,

I'm having some trouble getting the Form Validation Library to validate a file upload form.

My view file.
Code:
<?php echo form_open_multipart('uploads'); ?>

    <p>Name:</p>
    <p>&lt;input type="text" name="name" value="&lt;?php set_value('name'); ?&gt;"/&gt;
    &lt;?php echo form_error('name'); ?&gt;    
    </p>
    
    
    <p>Description:</p>
    <p>&lt;textarea rows="11" cols="27" name="description"&gt;&lt;?php set_value('description'); ?&gt;&lt;/textarea&gt;
    &lt;?php echo form_error('description'); ?&gt;
    </p>

    <p>Package:</p>
    <p>&lt;input name="package" type="file" /&gt;
    &lt;?php if(isset($error)) { echo $error; } ?&gt;
    </p>

    <p>Type:</p>
    <p><select name="type">
        <option value="1"&lt;?php echo set_select('type', '1'); ?&gt;>Logo</option>
        <option value="2"&lt;?php echo set_select('type', '2'); ?&gt;>Web design</option>
        <option value="3"&lt;?php echo set_select('type', '3'); ?&gt;>Artwork</option>
    </select>
    &lt;?php echo form_error('type'); ?&gt;
    </p>

    <p>&lt;input type="submit" name="submit" value="Upload" /&gt;&lt;/p>
&lt;/form&gt;

My Controller
Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Uploads extends Public_Controller
{
    
    var $data;
    
    function __construct()
    {
        parent::Public_Controller();
        $this->load->model('uploads_m');
        $this->load->helper(array('form', 'url', 'file'));
    }
    
    function index()
    {
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('name', 'Name of File', 'required');
        $this->form_validation->set_rules('description', 'File Description', 'required');
        $this->form_validation->set_rules('type', 'Type of File', 'required');
        $this->form_validation->set_rules('package', 'File', 'required|callback_check_file');
        
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('front/upload_form');
        }
        else
        {
            $this->load->view('front/upload_message', $data);
        }
    }
    
    function check_file()
    {
        $config['upload_path'] = APPPATH . 'uploads/';
        $config['allowed_types'] = 'zip';

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

        $field_name = "package";

        if ( ! $this->upload->do_upload($field_name))
        {
            $this->validation->set_message('_userfile',$this->upload->display_errors());

            return FALSE;
        }    
        else
        {    
            $this->data = array('upload_data' => $this->upload->data());

            $this->data['name'] = $this->input->post('name');
            $this->data['description'] = $this->input->post('description');
            $this->data['type'] = $this->input->post('type');
            $this->data['path_to_zip'] = $this->data['upload_data']['full_path'];

            $this->uploads_m->do_upload($this->data);

            return TRUE;
        }
    }
    
}
?&gt;

I have used a callback for the file upload field. This is really annoying because the file gets uploaded but no errors are shown, even when I only upload a file and leave all the other fields blank.

Any ideas?



Thanks guys.


Smile
#2

[eluser]Colin Williams[/eluser]
You might need to first inspect the validation object for previous errors before even attempting the upload. Don't have my IDE fired up at the moment but there should be some property of $this->validation that indicates a previous error. If not, you need to rethink your validation scheme: 1.) Run CI Validation, if passes, run file upload validation, if both pass, carry on.




Theme © iAndrew 2016 - Forum software by © MyBB