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

max: rule doesnt work as expected for files #25

Closed jakewhiteley closed 6 months ago

jakewhiteley commented 6 months ago

I am doing this:

$validation = (new Factory)->make($_POST + $_FILES, [
            'sectionId' => 'required|numeric',
            'portfolioImages' => 'max:5'
]);
$validation->validate();

But this doesnt validate the total count of uploaded files.

I could change it to be 'portfolioImages.name' => 'max:5' which would error if 6 images are uploaded, but I believe it is much more sensible behaviour for the max rule to work by validating the total count of files for that key.

jakewhiteley commented 6 months ago

Oh, actually ignore me - it does work, but some of my uploads were errors due to max_upload_size being reached. Closing issue.

dave-redfern commented 6 months ago

@jakewhiteley There is a rule to validate uploaded files, however if you are allowing multiple files, you should set this as an array and then add validation rules to each element e.g.:

[
    'portfolioImages' => 'sometimes|array|max:5',
    'portfolioImages.*' => 'uploaded_file|max:2M|mimes:jpeg,png',
]

I recommend checking the rule documentation for handling multiple file uploads.