Do I understand correctly that if file is not mandatory, then you can’t just use callback? The problem is that I have mandatory fields in the form and file is not mandatory, but I need to check that file is valid for upload. If it’s not valid, then I need to refill all field values (I do it with validation class) and show notification that file is not correct.
Yup. when I put all this in the callback, if the file is not the right type, it returns the error like normal. If you do not want the file upload to be manditory, in the callback, have an if statement checking if a file was selected if now, skip and return true, if not, run the callback like normal.
sorry to post in an old thread, but i dont see how pistolpete’s suggestion in post#2 can work. its very simple - the output is always that no file was uploaded. been breaking my head on this the past 4 hours. some official docs on how to handle this would be appreciated by everyone im sure.
Lets see some of your code. one thing that I changed was that I removed “required” from the form validation as this would return that no file was uploaded every time. instead, in the callback function the act of trying to upload the file wil check if a file was selected and if not it returns a false and an error of no file selected. so in my form rules, i only have the call back function for the image upload.
like i said post some code and we’ll see if we can work this out.
i should be thanking you im retrying with your suggestion of leaving out “required”. determined to get this done before the day’s end. ill post back shortly
ok here we go. these are two function in my controller. whether or not i pick a file for upload, i always get “You did not select a file to upload.”
im wondering - what do you use as the first parameter inthe set_rules function? the name of the field or ‘userfile’ (someone mention this was required, but what if you have multiple file upload fields?) or, $_FILES[‘prod_img’] ?
function _do_upload($file) { $config['upload_path'] = '/Applications/MAMP/htdocs/my-site/images/_uploads/';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) { //print "one"; exit; // set the validation error using the errors provided by the upload class $this->form_validation->set_message('_do_upload', $this->upload->display_errors()); return FALSE; } else { print "two"; exit; print_r($_FILES); exit; } }
editing my own posts here. it seems ‘userfile’ is the way to go as first param for the set_rules function. when using multiple fields, you can use this
ive certainly progressed, thanks! im just wondering now how to get $image_data = $this->upload->data(); back to the create() function from where the _do_upload callback is called.
i return $image_data, but its not available in create(). do you have to declare $image_data as private, public, ... ? if so, where?
it seems ‘userfile’ is the way to go as first param for the set_rules function. ...
again id like to stress that pistolpete’s solution in post#2 (what i was basing myself on all afternoon!) does NOT work.
This was almost a verbatim copy from one of my projects and it’s working fine there.
I doubt whether that is working because a file upload is saved in the $_FILES array but the input fields which are processed by the form validation class are in the $_POST array.
Could you please provide the full controller and view code?
stef25 - 12 April 2009 09:41 AM
do you have to declare $image_data as private, public, ... ? if so, where?
Have a look at this post: forums/viewreply/551725/
I’d use private as this is only controller internal data.
Don’t return $image_data in your callback function, save it in a class variable.
You can also process the form data that is passed to your callback and return it. If your callback returns anything other than a boolean TRUE/FALSE it is assumed that the data is your newly processed form data.
If you would like to capture the output of print_r(), use the return parameter. If this parameter is set to TRUE, print_r() will return its output, instead of printing it (which it does by default).
//send back to the view $attributes['prod_name'] = $this->input->post('prod_name'); $attributes['prod_descr_long'] = $this->input->post('prod_descr_long'); $attributes['prod_aff_link'] = $this->input->post('prod_aff_link');
Hey there!
Sorry to bother you guys with the same issue…
I’m basing my validation on PistolPete’s example and I’m always presented with the message:
“O campo Imagem é obrigatório.” which means: “The image field is required.”