pritam1605's avatar

Problems using nullable and image validation rule together

I am trying to upload an optional image to the back-end and am validating the image if it is uploaded. I am using the following rule:'channelImage' => 'nullable|image', I even tried replacing nullable with sometimes but to no avail. I used sometimes and nullable together, but that didn't work either.

Has anyone faced similar problem?

0 likes
10 replies
hdkaroh's avatar

I have the same problem here :disappointed: even tho my validation is declared to accept null values on the 'photo' field, is throwing me errors on image a mimes when no image selected.

this is my validation code:

$this->validate($request, [
    'firstName' => 'required',
    'lastName' => 'required',
    'phone' => 'nullable|unique:persons|max:10',
    'email' => 'nullable|email|unique:persons',
    'username' => 'nullable|unique:drivers',
    'photo' => 'nullable|image|mimes:jpeg,png|max:100',
    'vehicle' => 'nullable|integer',
    'password' => [
        'nullable',
        'confirmed',
        'min:8'
    ],
]);

and here my axios call:

createDriver : function() {
    let data = new FormData();

    for(var prop in this.driver) {
        if(this.driver[prop] != null) {
            data.append(prop, this.driver[prop]);
        }
        else { data.append(prop, ''); }
    }

    data.append('photo', this.fileSelected);

    axios.post('/drivers', data)
    .then((response) => {
        toastr.success('Driver created');
    }, (response) => {
        this.formMessages = response.response.data.errors;
    });
}

* fileSelected is declared null, and remains if not input given

any ideas?

cmdobueno's avatar

validation code

$this->validate($request, [
    'firstName' => 'required',
    'lastName' => 'required',
    'phone' => 'nullable|unique:persons|max:10',
    'email' => 'nullable|email|unique:persons',
    'username' => 'nullable|unique:drivers',
    'photo' => 'nullable|sometimes|image|mimes:jpeg,png|max:100',
    'vehicle' => 'nullable|integer',
    'password' => [
        'nullable',
        'confirmed',
        'min:8'
    ],
]);

Try out the magic that is 'sometimes'

https://laravel.com/docs/5.6/validation#conditionally-adding-rules

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:
hdkaroh's avatar

hi @cmdobueno, yeah, I've tried that already and got the same result. I read that sometimes is to use when we might not have the field present and nullable when the field is present but might be null, either option is not working, also tried together and nothing, I really have no idea why is not working :(

thanks so much for your feedback anyways! :D

fadykstas's avatar

The solution is not to append 'photo' to FormDate if null, otherwise it will append it as 'photo' = 'null', instead of 'photo' = null.

5 likes
Gabotronix's avatar

@FADYKSTAS - This should be the chosen solution since it solved the issue for me:

if(this.$refs.image.files[0]){bulletin.append('image', this.$refs.image.files[0])};

FormRequest:

return [
            'title' => 'required|min:5|max:100',
            'body' => 'required|min:5|max:250',
            'image' => 'nullable|sometimes|image|mimes:jpeg,bmp,png,jpg,svg|max:2000',
        ];
1 like
goranferbisek's avatar

Thank you @fadykstas . I had the same problem as @hdkaroh . i was focused so much on the back end that I forgot about the front end. I was passing the optional image to axios even if it was not uploaded

goran1301's avatar

I got the same problem, but there is one small detail. For example, if you have a request from frontend on jquery, you don't get null, but you get 'null' - this is STRING, but not actual null. You can replace this string by real null in validationData() method in your request class. It works fo me.

But better way - to have a good frontend with correct data types :)

1 like
Alessio_Pellati's avatar

well, actually nullable doesn't work with the input type file but you can use exclude_unless in the validation rule to make the exact same thing (You can find the usage on laravel validation ) like this :

'photo' => 'exclude_unless:photo,null|image|mimes:png,jpeg,jpg|max:5120'

Please or to participate in this conversation.