Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
$_FILES validation?
  • If I want to check to use the validation class to add a rule ensuring that someone is uploading a file, how would I do that? Something like this: $val->add('media_upload', $data)->add_rule('required'); Does not work, because the $val class only checks the $_POST array. Am I using the wrong thing? Should I extend the validation class? Seems like something that would already be covered.
  • You can use the upload class for checking uploaded files. You can set things like "max_size", "max_lenght" etc. For more information: http://fuelphp.com/docs/classes/upload/config.html
  • I'm already using the upload class, but the Validation class gives me $val->errors() function. I need to know how to create a validation rule for $_FILES array, not that I can use the Upload class. I do appreciate your help, though = )
  • So actually all you're after is an error message? Since you'll have to process (and possibly store) validation error messages as well, what's wrong with
    if (empty($_FILES))
    {
        // no files uploaded message here
    }
    else
    }
        // use the Upload class to process them
    }
    
    and don't forget you'll have to check the errors from the Upload class to...
  • Correct me if I'm wrong, but doesn't it make more sense that the validation class should handle ALL user input, and further processing using Upload class should only happen after initial validation is passed? I COULD do that, but it doesn't seem very elegant. I want to do this: $val->add('upload_field', 'My Upload')->add_rule('required'); Then, when I feed $val->errors to my template there's only one array. What you're suggesting requires the following: $data = array(); $val->add('non_upload_rule', 'I\'m a regular form field')->add_rule('required'); array_push($data, $val->errors()); if(empty($_FILES)
    {
    $empty_upload_error = array('myfieldname' => 'File for My Field Name must be uploaded');
    } foreach( Upload::get_errors() as $upload_error )
    {
    array_push($data, $upload_error)
    } That just seems kind of dirty. Maybe I'm just picky.
  • I found myself having to check further into the $_FILES array structure to make any use of the $_FILES array...
    if (! empty($_FILES['field']['tmp_name'])
    {
        // Do something.
    }
    

    I can't seem to find it, but I read in the comments on the php.net docs that it is unreliable to check the $_FILES array directly... unless that's been fixed and just lives on in the comments.
  • Just found this thread after trying to make the file field required and seeing that the validation class does not recognize when a file has been selected. I agree that it would be nice if the validation class could accurately check. I'm using the Upload::get_files() function to check, but not being able to add the normal "required" validation messes up the automatic "file is required" that's part of the form template.
  • In PHP uploaded files are not part of the form data, so the validation class doesn't have any info on the form field.
  • Yes, but couldn't the validation class check the $_FILES array or use the Upload class to verify that a file has been selected? I assume that's what most of us are doing now with added code. Also, currently, if the file field is "required," then the validation class always indicates a validation error even when a file has been selected, so the required attribute cannot be used. This also means that the html5 checking of required cannot be used. If the validation class does not use the $_FILES array or Upload class, then maybe the validation class should not check required attribute for files.
  • You get this validation error because, as I wrote before, a 'file' input field isn't part of the form data. So validation will give you the error because the field is missing from $_POST. Validation is on the roadmap for 2.0 to be refactored, so if you feel this is a missing feature, create a feature request on http://github.com/fuel/core/issues, with a use case.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion