Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel mimes validation always called even if no input file is uploaded (Not used required rule)

I have used single validation rule only for file input field. That is mimes rule. I wanted to skip this rule if no file is uploaded, so i have not used 'required' rule. But it is always showing mime type message even if no file is uploaded. I have just added required rule for testing , at that time it shown required error message. This problem is only when submitting form using ajax with jquery.form.js

like image 659
Abdul Munzeer B M Avatar asked Nov 24 '25 20:11

Abdul Munzeer B M


2 Answers

You can use the sometimes validation type for that.

'file' => 'sometimes|mimes:jpeg,png'

Validating When Present

In some situations, you may wish to run validation checks against a field only if that field is present in the input array. To quickly accomplish this, add the sometimes rule to your rule list.

Official Laravel docs

like image 180
Leguam Avatar answered Nov 27 '25 13:11

Leguam


You can use sometimes rule.

'photo' => 'sometimes|mimes:jpeg,bmp,png'

You can check more about it here https://laravel.com/docs/5.4/validation#conditionally-adding-rules

like image 29
Sandeesh Avatar answered Nov 27 '25 13:11

Sandeesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!