Amit Kumar
Amit Kumar

Reputation: 143

Laravel file validation issues

I have a form to upload image field optional, Laravel validation working fine but same I have another form to update where also image field optional but here Laravel validation not working fine if I don't upload the file I am getting an error.

The file must be must be an image

Here is my rules

case 'PATCH':
        {
            return [
                'name' => 'required|regex:/^[\pL\s\-]+$/u|min:1|max:255',
                'file' => 'image|mimes:jpeg,bmp,png,jpg|max:5000'
            ];
        }

Where am I doing a mistake?

Upvotes: 1

Views: 1292

Answers (2)

Peter Kota
Peter Kota

Reputation: 8340

You should extend your validation rule:

'file' => 'nullable|image|mimes:jpeg,bmp,png,jpg|max:5000'

You can read about nullable in Laravel docs.

Upvotes: 5

Eternal1
Eternal1

Reputation: 5625

You should add nullable as another rule, if the file is optional, otherwise image validation will indeed fail. Also, you don't need to use both image and mimes validation, they are interchangeable.

Upvotes: 3

Related Questions