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

gRoberts's avatar

Laravel 6.x - Validate date before/after only if other field has been populated?

Hey all,

Either going off caffeine since Christmas, or waking up too early has affected my brain this morning but I'm sure I'm missing something.

I have two date fields that specify the start/end of a period, which are both optional, however if you provide either, they must start < end.

$rules = [
    'moved_in_at'  => ['nullable', 'before:moved_out_at'],
    'moved_out_at' => ['nullable', 'after:moved_in_at']
];

If I only populate the moved_in_at value, it should pass validation however it's failing and expecting the moved_out_at to exist.

On a side note, I created a custom rule to deal with it, but found there is nothing really in the documentation to help you go through getting the second field's name into the validation message.

What am I missing?

0 likes
2 replies
bobbybouwmann's avatar
Level 88

In this case, you only need the after:moved_in_at. Since you don't supply the moved_out_at value, it has nothing to check against.

$rules = [
	'moved_in_at'  => ['nullable'],
	'moved_out_at' => ['nullable', 'after:moved_in_at'],
];
gRoberts's avatar

Simple huh... Maybe I should start drinking coffee again!

Thank you for your help!!

1 like

Please or to participate in this conversation.