I have a form with some inputs and one file input.
Everything works well but there is a weird problem!
Here is my validations code:
$validator = \Validator::make($request->all(), [
'name' => 'required|max:25|min:3',
'email' => 'required|email|max:35|min:5',
'phone' => 'required|max:15|min:7',
'file' => 'max:2000|mimes:jpeg,png,doc,docs,pdf',
]);
The problem is that I don't set required for file but I don't know why when I submit the form it said:
The file must be a file of type: jpeg, png, doc, docs, pdf
Actually I want to file be optional.
I have tried sometimes:
'file' => 'sometimes|max:2000|mimes:jpeg,png,doc,docs,pdf',
But it didn't work.
Thanks for any suggestion.
Update:
my Ajax request:
$(document).ready(function () {
$("#submit").click(function (event) {
event.preventDefault();
var formData = new FormData();
formData.append('name', $('#name').val());
formData.append('email', $('#email').val());
formData.append('phone', $('#phone').val());
formData.append('telegram', $('#telegram').val());
formData.append('contactWith', $('#contactWith').val());
formData.append('orderType', $('#orderType').val());
formData.append('price', $('#price').val());
formData.append('uiLevel', $('#uiLevel').val());
formData.append('codingType', $('#codingType').val());
formData.append('maxTime', $('#maxTime').val());
formData.append('file', $('#file')[0].files[0]);
formData.append('message', $('#message').val());
$.ajax({
type: "POST",
url: "{{route('orders.store')}}",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
},
data: formData,
contentType: false,
processData: false,
success: function (msg) {
},
}, "json")
})
});
And my file input:
<div class="form-group">
<label for="file">send file</label>
<br>
<label id="browse-label" for="file"><i class="fa fa-upload" aria-hidden="true"></i>browse
<input type="file" id="file">
</label>
<span id="filename">no file chosen</span>
</div>
Use the nullable rule
Docs: https://laravel.com/docs/8.x/validation#a-note-on-optional-fields
Which gives you:
'file' => 'nullable|max:2000|mimes:jpeg,png,doc,docs,pdf',
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With