somnambulist-tech / validation

A re-write of rakit/validation, a standalone validation library inspired by Laravel Validation
MIT License
44 stars 13 forks source link

Uncaught TypeError: is_uploaded_file() must be of type string #8

Closed danFWD closed 1 year ago

danFWD commented 1 year ago

When using a file input with the 'multiple' attribute, you must add a validation rule with the dot syntax e.g. photos.* or it throws an error if one or more files is attached:

Uncaught TypeError: is_uploaded_file(): Argument #1 ($filename) must be of type string, array given in .../somnambulist/validation/src/Rules/Behaviours/CanValidateFiles.php:11

I assume this is expected behavior, but the uncaught error should probably return an exception.

In addition, when requiring a file to be attached to a multiple-file input, it does not throw a validation error if no file is attached: <input type="file" name="photos" multiple accept=".jpg,.jpeg" />

// expected behavior is to throw validation error when no file is attached:
$validation = [
  'photos.*' => 'required|uploaded_file:0,1M,jpeg',
];

Am I interpreting or writing the rule wrong, or am I correct in that this is a bug?

danFWD commented 1 year ago

EDIT: Scratch the last part about the required rule not working. After review, it does actually work.

dave-redfern commented 1 year ago

@danFWD Thanks for reporting this issue.

The error you are seeing happens because the _FILES array is not being converted to a normalised array format. That only happens when dot notation is used. Examples for handling multiple file uploads is documented in the current readme, however: there is no explicit warning about requiring the use of dot notation on the attribute name.

I will update the docs accordingly and add an exception in the file validation trait to guard against this scenario.

dave-redfern commented 1 year ago

1.4.2 will now raise an exception if the file data appears as an array and the docs have been revised to make it clear that dot notation is needed.