5.3 image validation only if image is uploaded

Published 1 year ago by lorvent

In 5.3, How can i validate an image only if its uploaded.

a rule like 'pic_file' => 'mimes:jpg,jpeg,bmp,png|max:10000', would validate pic_file only if its uploaded in 5.2

but in 5.3, i am getting error as image extension is wrong eventhough i didn't upload image.

how can i make validation optional? or how i can add a rule dynamically in Requests (not in controller)

Thanks.

tisuchi
tisuchi
1 year ago (296,925 XP)

I think the rule should like so-

'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'

Can be helpful this tutorial- http://itsolutionstuff.com/post/laravel-53-image-upload-with-validation-exampleexample.html

lorvent
lorvent
1 year ago (13,385 XP)

that makes image required, i want image to be optional and if its uploaded then it should have only allowed extensions.

tisuchi
tisuchi
1 year ago (296,925 XP)

Ok, what I meant that...

'image' => 'image|mimes:jpeg,png,jpg,gif,svg'
lorvent
lorvent
1 year ago (13,385 XP)

not working either :(

kfirba
kfirba
1 year ago (213,625 XP)

@lorvent try:

'pic_file' => 'sometimes|mimes:jpg,jpeg,bmp,png|max:10000'
tisuchi
tisuchi
1 year ago (296,925 XP)

Can you show you code?

hitender_dargash

Laravel treat empty strings as null. when you provide nothing it treat its as null and validations work. So you need to provide nullable validator also like this 'profile_pic'=>'nullable | image'

Please sign in or create an account to participate in this conversation.