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
4 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.
Comments
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
1 Comment
Abdul Munzeer B M
Still not working in my form. I think the problem is when using ajax form submission using jquery.form js
I've issued the same problem recently with ajax uploading, and handled the file validation using
public function rules()
{
$rules = [
'invitation_message' => 'required'
];
if (request()->hasFile('invitation_excel_sheet')) {
$rules = array_merge($rules, [
'invitation_excel_sheet' => 'mimes:xlsx,csv'
]);
}
return $rules;
}
sometimesto your rule.