[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><input type="text" name="name" value="<?php set_value('name'); ?>"/>
<?php echo form_error('name'); ?>
</p>
<p>Description:</p>
<p><textarea rows="11" cols="27" name="description"><?php set_value('description'); ?></textarea>
<?php echo form_error('description'); ?>
</p>
<p>Package:</p>
<p><input name="package" type="file" />
<?php if(isset($error)) { echo $error; } ?>
</p>
<p>Type:</p>
<p><select name="type">
<option value="1"<?php echo set_select('type', '1'); ?>>Logo</option>
<option value="2"<?php echo set_select('type', '2'); ?>>Web design</option>
<option value="3"<?php echo set_select('type', '3'); ?>>Artwork</option>
</select>
<?php echo form_error('type'); ?>
</p>
<p><input type="submit" name="submit" value="Upload" /></p>
</form>
My Controller
Code:
<?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;
}
}
}
?>
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.