CodeIgniter Forums
Required Validation for file field - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Required Validation for file field (/showthread.php?tid=32535)



Required Validation for file field - El Forum - 07-26-2010

[eluser]_steve[/eluser]
I'm trying to apply required validation to a file upload field.
Usually on a text field this would be:
Code:
$this->form_validation->set_rules('name', 'Name', 'required');
However if I try this on the userfile field, it returns an error, whether a file was selected for upload or not. Other plain text fields in the same form are behaving as expected.

Any ideas?


Required Validation for file field - El Forum - 07-26-2010

[eluser]mi6crazyheart[/eluser]
Can't understand , if u r uploading any file then why using form validation class rather file uploading class...

For file uploading: http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html


Required Validation for file field - El Forum - 07-27-2010

[eluser]gyo[/eluser]
Have you tried on different browsers?
Sometimes there are problems with the MIME types.

Hope it helps!


Required Validation for file field - El Forum - 07-27-2010

[eluser]danmontgomery[/eluser]
file uploads are passed through $_FILES, not $_POST or $_GET as other form data, so this will not work.


Required Validation for file field - El Forum - 07-27-2010

[eluser]_steve[/eluser]
Thanks for the replies.

I'm using the validation class because I want to validate all the fields before I bother uploading the image.

I've tried on different browsers and get the same result.

Taking what noctrum said about $_FILES, I tried to create a custom validation rule, however its still not working, anyone see where I'm going wrong?

Code:
//... from a validate function with the other rules
$this->form_validation->set_rules('userfile', 'Image', 'callback_validateImage');

function validateImage() {
        if($_FILES['userfile']['size'] == 0) {
            $this->form_validation->set_message('userfile', 'You did not upload an image.');
            return FALSE;
        } else {
            return TRUE;
        }
}



Required Validation for file field - El Forum - 08-26-2010

[eluser]Unknown[/eluser]
Hi!
You need to put this function in a controller| helper and try to call it by callback_.
It will work


Required Validation for file field - El Forum - 08-26-2010

[eluser]davidbehler[/eluser]
As far as I know you can't validate file uploads using the form_validation library.

You should use the upload library for that or search the forum, because someone created an extended version of the form_validation library that supports file upload validation. Maybe this is what you are looking for: http://ellislab.com/forums/viewthread/123816/