When I first got started using Laravel and I had to validate dates, my first instinct would always be to jump straight into a custom validator rule to control what was happening. However the majority of the time it was a very simple validation I was doing i.e making sure a date was after today.
Date Validators
Laravel has these handy validators when it comes to dates, I had recently used them when creating an invite crud system for an application and I wanted to ensure that the invite expiry date was sometime after today. Sure I could whip up a validator / custom rule to parse the date provided in the request and ensure that its greater than today or I could simple use after:today
Yep, Laravel makes it that simple, now let me show you it in some context so its easier to understand.
'expires_at' => 'required|date|after:today'
This was all I needed to ensure that my dates couldnt be set to a time in the past for their expiry. You can do things such as
- before:today
- after:tomorrow
- after:today
You can of course reference these in the Laravel Documentation.