[eluser]LuckyFella73[/eluser]
Quote:I think you don’t need to use callback function coz file_upload has its own validation.
The callback function would use the CI upload class and that way the CI validation.
If you don't set up a callback function you end up with multiple IF statements
in case the file is a "requiered" field. You have to compare if the "normal"
input field are valid, then if the file upload went fine and compare both results.
When using a callback function you have all validation in one place.
Could look like:
Code:
<?php
function newproduct()
{
// Validation Rules
$this->form_validation->set_rules('title', 'Title', 'trim');
$this->form_validation->set_rules('description', 'Description', 'trim|required|max_length[120]');
$this->form_validation->set_rules('image', 'Image', 'trim');
$this->form_validation->set_rules('price', 'Price');
$this->form_validation->set_rules('userfile', 'Your file', 'callback__do_upload_file'); // file would be requiered in this example
$this->id = $this->uri->segment(3);
$data['id'] = $this->id;
// IF IS FALSE SHOW FORM
if ($this->form_validation->run() == FALSE)
{
$this->load->view('admin/newproduct');
}
//ELSE INSERT INTO DB
else
{
$data = array(
'title' => $this->input->post('title'),
'description' => $this->input->post('description'),
'price' => $this->input->post('price'),
'status' => $this->input->post('status')
);
$this->db->insert('ci_products', $data);
redirect('admin/products');
}
}
function _do_upload_file()
{
$config['upload_path'] = 'YOUR_PATH';
$config['allowed_types'] = 'ALLOWED_TYPES';
$config['max_size'] = 'YOUR_MAX_SIZE';
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());
return FALSE;
}
else
{
$this->filedata = $this->upload->data(); // use $this->filedata for DB insert - like filename or whatever you need
return TRUE;
}
}
Don't forget to look at the user guide.