lorvent's avatar

5.3 image validation only if image is uploaded

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.

0 likes
7 replies
lorvent's avatar

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

tisuchi's avatar

Ok, what I meant that...

'image' => 'image|mimes:jpeg,png,jpg,gif,svg'
kfirba's avatar

@lorvent try:

'pic_file' => 'sometimes|mimes:jpg,jpeg,bmp,png|max:10000'
3 likes
hitender_dargash's avatar

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'

1 like

Please or to participate in this conversation.