Skip to content

Conversation

@calebdw
Copy link
Contributor

@calebdw calebdw commented Dec 9, 2025

Hello!

Motivation

I just had a bug in production where a developer (me 🙃) 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:

  • beforeNow
  • afterNow
  • nowOrBefore
  • nowOrAfter
  • andTime

Thanks!

@taylorotwell
Copy link
Member

@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())],

@calebdw
Copy link
Contributor Author

calebdw commented Dec 10, 2025

@taylorotwell

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:

protected function formatDate(DateTimeInterface|string $date): string
{
return $date instanceof DateTimeInterface
? $date->format($this->format ?? 'Y-m-d')
: $date;

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()],

@taylorotwell
Copy link
Member

Could you just add a method to the Rule class something like this:

public static function dateTime()
{
    return (new Date)->format('Y-m-d H:i:s');
}

@calebdw calebdw force-pushed the calebdw/push-snpxxsqzoxsv branch from 781ac69 to 1a08132 Compare December 10, 2025 17:03
@calebdw
Copy link
Contributor Author

calebdw commented Dec 10, 2025

Sure, I can do that. Would you still be opposed to the now methods? They are a mirror of the today methods (e.g., todayOrBefore), or maybe something like inFuture or inPast?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants