-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[12.x] feat: add now methods to Date rule #58059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@calebdw can you give me a bit more detail on why your original attempt didn't work for you? 'request_at' => [Rule::date()->beforeOrEqual(now())], |
|
Yes, so when a carbon instance is passed it's truncated to just the date, but when the string is passed it gets resolved to a full datetime later on: framework/src/Illuminate/Validation/Rules/Date.php Lines 128 to 132 in 0a24afd
The only two possible fixes are: 'request_at' => [Rule::date()->format('Y-m-d H:i:s')->beforeOrEqual(now())],
// or
'request_at' => [Rule::date()->beforeOrEqual('now')], // do not use now()both of which are not really as clean as they could be: 'request_at' => [Rule::date()->andTime()->nowOrBefore()], |
|
Could you just add a method to the public static function dateTime()
{
return (new Date)->format('Y-m-d H:i:s');
} |
781ac69 to
1a08132
Compare
|
Sure, I can do that. Would you still be opposed to the now methods? They are a mirror of the |
|
@calebdw can we use the naming already used by now()->isPast(); // beforeNow
now()->isFuture(); // afterNow
now()->isNowOrPast(); // nowOrBefore
now()->isNowOrFuture(); // nowOrAfterIMO, they would be easier to remember for those familiar with these methods. |
|
I'm not opposed to that, just want to hear @taylorotwell 's thoughts |
|
Thank you sir! |
* feat: add now methods to Date rule * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
Hello!
Motivation
I just had a bug in production where a developer (me 🙃) was validating a datetime with:
However, this caused failures when the datetime was the current day and had a time component. The fix was:
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
This adds the following methods to the Date validation rule:
beforeNowafterNownowOrBeforenowOrAfterandTimeThanks!