Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make the file optional when using laravel's file validation

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>
like image 790
Root Man Avatar asked May 09 '26 15:05

Root Man


1 Answers

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', 
like image 171
Tofandel Avatar answered May 12 '26 12:05

Tofandel