Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BastianSchur's avatar

Validate date if available

Hey all,

i've a problem with the laravel validator. I want to validate a date, if it's available. I use the 'date' Validator, but it get an validation error if the column is not given. Is there a workarround without custom validations?

Bastian

0 likes
6 replies
ejdelmonico's avatar

Without seeing your code, it sounds like you have the required rule for the input.

thomaskim's avatar

No code = no help.

You have to help us help you.

pmall's avatar

Because you probably send an empty string, which is obviously not a date.

Try to use the sometimes rule : 'sometimes|date' and to parse your inputs to set the date input to null when it is an empty string.

BastianSchur's avatar

@pmall: That is what i'm searching for! But it doesn't work! I use the Rule 'sometimes|date' and create the Object without the column in my array. I get the same error message!

pmall's avatar

Yeah because sometimes skips the validation when the field is not present as a key in your input array (ex: field is disabled).

You have two way of handling this :

  • Disable the date field by default and put a checkbox with some javascript for enabling it when checked. So if no date need to be specified, the checkbox is unchecked and the date field is disabled. Sometimes will skip the validation.

  • Override the all method of your form request and return the input array without the date key when date == ''.

1 like

Please or to participate in this conversation.