Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't validate input file in laravel

I want to validate a file input but the validation does not work it always return The given data was invalid. even if the file given meets the validation requirements knowing that I'm uploading multiple files at once:

Here's the validation I use :

    Validator::make($request->all('files'), [
        'files' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ])->validate();

And here's the HTML I use :

<input type="file" id="files" ref="files" multiple v-on:change="uoloadFiles()" name="files"/>

Knowing that I use vue to handle the form submission.

Thanks for helping

like image 222
Rahmani Seif El Moulouk Avatar asked Sep 21 '25 03:09

Rahmani Seif El Moulouk


1 Answers

This should work since you are uploading multiple files, each file should be validated separately.

Validator::make($request->all('files'), [
    'files.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
])->validate();

Note the *.

Further reference here

like image 115
Imesha Sudasingha Avatar answered Sep 23 '25 00:09

Imesha Sudasingha