gRoberts
2 months ago

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

Posted 2 months ago by gRoberts

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?

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