framework
framework copied to clipboard
[12.x] feat: add now methods to Date rule
Hello!
Motivation
I just had a bug in production where a developer (me :upside_down_face:) was validating a datetime with:
'request_at' => [Rule::date()->beforeOrEqual(now())],
However, this caused failures when the datetime was the current day and had a time component. The fix was:
'request_at' => [Rule::date()->beforeOrEqual('now')], // do not use now()
which is not very elegant and requires a comment to prevent future develepers from causing a regression.
Additionally, we have a ton of datetime fields that need to be validated and don't want to have to repeatedly specify ->format('Y-m-d H:i:s')
Solution
What I would like is
'request_at' => [Rule::date()->andTime()->nowOrBefore()],
This adds the following methods to the Date validation rule:
beforeNowafterNownowOrBeforenowOrAfterandTime
Thanks!