Welcome Guest, Not a member yet? Register   Sign In
Required Validation for file field
#1

[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?
#2

[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-gui...ading.html
#3

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

Hope it helps!
#4

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

[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;
        }
}
#6

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

[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/




Theme © iAndrew 2016 - Forum software by © MyBB