Laravel 5.8.17 - validation image not working as expected

I'm creating Laravel validation, and meet strange issue.

I have a form with input type file, where I can attach multiple files.

<input type="file" name="img[]" class="form-control" multiple="multiple">

In the Controller I have validation rules:

$validationRules = [
   'img' => 'required|image'

And all the time my validation doesn't work, I always get a message: "The img must be an image."

There is no difference when I upload 1 file or 2 or 3 at the same time - error is the same. There are another fields at the same form (text fields) and all is ok with them, problem appears only with image. Tried to remove "image" from the validation and replace it with mimetypes or mimes - anyway system can't recognize image.

Please advice what could be a problem here, thanks a lot.

2 answers

  • answered 2019-07-18 12:56 Mohammed Aktaa

    You need to make validation attribute img to img.*

  • answered 2019-07-18 13:04 Gowthaman D

       $input_data = $request->all();
       $validator = Validator::make(
            $input_data, [
            'img.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
                'img.*.required' => 'Please upload an image',
                'img.*.mimes' => 'Only jpeg,png and bmp images are allowed',
                'img.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',