How do I write a before-or-equal-date validation that compares to the current date?
Asked Answered
N

2

6

I am new to Laravel and am using Laravel 6.x. I need to write a validation that looks at a date supplied by a user on a form (via a JQuery datepicker) to make sure the user chose a date that is on or before the current date. I can't figure out how to express the current date without using apostrophes in my expression somewhere and THAT is going to mess with Laravel understanding the validation properly.

For example, if I write:

'sleep_date' => ['required', 'before-or-equal-date: Date(Y-m-d)']

that's not going to work because there are no apostrophes around the argument of the Date function. But if I add apostrophes as in this example, Laravel says there is a syntax error:

'sleep_date' => ['required', 'before-or-equal-date: Date('Y-m-d')']

How do I code this so that it works as intended?

Namesake answered 22/2, 2020 at 17:41 Comment(0)
I
6

You can concatenate the current date with a dot:

'sleep_date' => ['required', 'before_or_equal:' .  Date('Y-m-d')]

Also you can use the laravel now() helper instead Date() in the same way:

'sleep_date' => ['required', 'before_or_equal:' . now()->format('Y-m-d')]
Intrusive answered 22/2, 2020 at 17:57 Comment(6)
No worries, I didn't use that rule for a while and went checking in docs. ;)Toomin
@Toomin - where did you find that in the docs? I didn't find any examples of this situation in the docs, at least not in the validations page, which is where I would have expected to find it.Namesake
@Namesake you can find the docs under Validation Available Validation Rules: before_or_equal:date there is no explicit example, just an explanation of how it worksSergio
@porloscerros - I had already found that page, no problem. I couldn't see any way to incorporate a current date in the validation so I was wondering where you and/or Tpojka found the technique of using the concatenation operator. Is that in the manual somewhere?Namesake
@Namesake Oh well, I incorporated that technique with experience in php. Since in the documentation says "The field under validation must be a value preceding or equal to the given date.", I pass the date to the rule after the : concatenating the current date. And since Laravel brings Carbon as a dependency, I usually use carbon to work with dates and time, or now() helper if I just need Carbon:now().Sergio
@porloscerros - That makes sense. Laravel code looks so different from regular PHP code that I find myself forgetting that it uses PHP and can use the same techniques, like concatenation with the dot operator.Namesake
M
1

You can also use before_or_equal:today

Not sure what Laravel Version that was added though.

Minimum answered 29/7, 2024 at 14:47 Comment(3)
If you check the documentation, there's a version dropdown. before_or_equal:today should be valid in Laravel 6.x: laravel.com/docs/6.x/validationFantasist
yesterday, tomorrow, now alsoMinimum
Yup! As stated in the docs, "date (the value after :) is passed to strtotime()", so if strtotime('today') is valid, before_or_equal:today is also valid. Replacing today with yesterday, tomorrow and now all return values (invalid values return false), so all of those are valid 🙂Fantasist

© 2022 - 2025 — McMap. All rights reserved.