4

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

3 Answers3

6

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

Imesha Sudasingha
  • 3,462
  • 1
  • 23
  • 34
0

if you have only this field you can do:

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

otherwise use all method without arguments:

 Validator::make($request->all(), [
      'files.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
 ])->validate();
Amr Aly
  • 3,871
  • 2
  • 16
  • 33
0

try

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

i dont think you need image just mimes is fine