0
votes

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?

1
Have you tried using the image validation rule ?Nazgot
Haven't you forgot to add the enctype="multipart/form-data" in your form?Ruub
can u post the full form?Wailan Tirajoh
Thanks everyone. I actually forgot the enctype in my form. @Ruub special thanks to you.Raju Ahmed

1 Answers

0
votes

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.