Martin Bean
1 min readMay 31, 2020

--

Laravel already offers functionality out of the box to do things like this.

From https://laravel.com/docs/7.x/validation#conditionally-adding-rules:

In some situations, you may wish to run validation checks against a field only if that field is present in the input array.

So if you want to only check that ends_at is “not required, but if filled must be a date and after starts_at” then you can do that with the sometimes rule.

'ends_at' => ['sometimes', 'date', 'after:starts_at']

--

--

Martin Bean
Martin Bean

Written by Martin Bean

Software engineer and full-stack web developer.

No responses yet