-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[11.x] Add Customizable Date Validation Rule with Flexible Date Constraints #53465
base: 11.x
Are you sure you want to change the base?
[11.x] Add Customizable Date Validation Rule with Flexible Date Constraints #53465
Conversation
It's cool that it allows all the before/after/between validators to work with custom formats. Although in most cases I'd suggest normalizing the date format before validation instead. It's not that cool that this seems to duplicate all the code. Is it worth maintaining two separate implementations of each rule? |
Couple things come to mind. What about "before / after today" but inclusive of today? What about being about to pass Carbon / DateTime instances? |
Hi @taylorotwell, Thanks for bringing this up! I’ve made a few adjustments based on your feedback: Carbon/DateTime Support: Enhanced the Let me know if this looks good or if there’s anything else you’d like to discuss. Thanks again for the insights! |
Unfortunately, normalization is out of scope here since, while FormRequests have a |
How do we need to think about / validate hours / minutes / seconds with this PR? For example, what if I want to say a date / time combination is simply in the future, which would need second level validation? |
This pull request introduces a new, customizable
Date
validation rule for Laravel. TheDate
rule simplifies date validation by offering chainable methods that allow for complex date requirements directly within the rule definition. This enhancement includes methods to specify date formats, enforce that dates are before or after specific dates (including "today"), and to ensure dates are on or before/after specified constraints.With this addition, developers can create readable and flexible date validation rules without having to write multiple rules separately in the validation array.
Features
format($format)
: Specifies a date format for validation. Defaults to'Y-m-d'
if not specified.afterToday()
: Ensures the date is after today’s date.beforeToday()
: Ensures the date is before today’s date.after($date)
: Ensures the date is after the specified date.afterOrEqual($date)
: Ensure the date is on or after the specified date.before($date)
: Ensures the date is before the specified date.beforeOrEqual($date)
: Ensure the date is on or before the specified date.afterOrEqual($date)
: Ensures the date is on or after the specified date.beforeOrEqual($date)
: Ensures the date is on or before the specified date.between($from, $to)
: Ensure the date is between two dates.betweenOrEqual($from, $to)
: Ensure the date is between or equal to two dates.Example Usage
Here is an example of how to use the
Date
rule in a Laravel form request:Benefits
This
Date
rule class: