Skip to content
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

[10.x] Fix date validation issues #49994

Closed
wants to merge 4 commits into from
Closed

[10.x] Fix date validation issues #49994

wants to merge 4 commits into from

Conversation

mateusjunges
Copy link
Contributor

@mateusjunges mateusjunges commented Feb 7, 2024

Fixes #49955
Fixes #49988

This is an alternative to #49956

In Laravel v10.42.0, we can validate that a date is after or before another one, adding/subtracting a given amount of minutes, hours, seconds:

$data = [
    'dt1' => '2020-01-01',
    'dt2' => '2024-01-01',
];

Validator::make($data, [
    'dt1' => 'required|date',
    'dt2' => 'required|date|after:dt1 +1 day', //dt +1day also works
]);

PR #49871 broke this behavior and the nested after date validation.

I tried to fix both issues without reverting the original PR. Also added tests to make sure after and before are working as expected now.

$comparedValue = $this->getValue($parameters[0]);

if (! is_string($comparedValue) && ! is_numeric($comparedValue) && ! $comparedValue instanceof DateTimeInterface) {
if (count($dateAttributes) > 1) {
Copy link
Contributor Author

@mateusjunges mateusjunges Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just explaining why count() > 1 is enough here. We have the following options:

User validates using after:date_field

This is the most straight forward case. The $dateAttributes array contains only one item and we don't execute the if block.

["date_field"] // count = 1

User validates after:date_field +1day

Now, $dateAttributes contains this array:

["date_field", "+1day"] // count = 2

User validates with after:date_field +1 day

In this scenario, $dateAttributes will be

["date_field", "+1", "day"] // count = 3

User validates with after:date_field + 1 day

Here, $dateAttributes contains

["date_field", "+", "1", "day"] // count = 4

@mateusjunges mateusjunges changed the title [10.x] Fiix date validation issues [10.x] Fix date validation issues Feb 7, 2024
@taylorotwell
Copy link
Member

Reverted original PR. Will release that before trying again.

@mateusjunges
Copy link
Contributor Author

@taylorotwell should I send this again once you release the new version?

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.

Error in rule "after" date validation [BUG] Nested after date validation is broken
2 participants