3

I am trying laravel image validation for the following field-

<label class="btn btn-success">
    <input name="image" type="file"/>Add Image</label>
    @if ($errors->has('image'))
       <span class="alert alert-danger">
          {{ $errors->first('image') }}
       </span>
    @endif

And my validation rule is written like this-

$request->validate([
  'image'=> 'mimes:jpeg,jpg,png,gif|max:1000',
]);

But no matter what whether I give a valid image as input or not my validation rule gives me this error-

The image must be a file of type: jpeg, jpg, png, gif.

You can consider me beginner in laravel and I cant really find out what I am doing wrong here. Can anyone help?

4
  • Have you tried using the image validation rule ? Commented Apr 23, 2021 at 10:48
  • 1
    Haven't you forgot to add the enctype="multipart/form-data" in your form? Commented Apr 23, 2021 at 10:51
  • can u post the full form? Commented Apr 23, 2021 at 10:54
  • 2
    Thanks everyone. I actually forgot the enctype in my form. @Ruub special thanks to you. Commented Apr 23, 2021 at 11:06

1 Answer 1

4

Try this set of rules for your image input field:

'image' => 'nullable|image|mimes:jpeg,jpg,png,gif',

This will:

  • make the field nullable, so you can send it as null
  • image to validate the filetype (see doc)
  • mimes:jpeg,jpg,png,gif that will check for specific file mime type (doc) (also check mimetypes)

You can even check image idmension. Take a look at available rules, custom validation rules and the intervention image package.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.