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

[5.4+] Fixed Validator failing on 'before_or_equal:today' for today's date #20868

Closed
wants to merge 2 commits into from

Conversation

pantherdd
Copy link
Contributor

[ This is a follow-up for the prematurely closed PR #20789 . ]

Fixed Validator failing on 'before_or_equal:today' when the input is today's date and a format is specified

The issue was that the time part was bleeding through from the current timestamp in DateTime::createFromFormat.

Test case:

  • Validation rule: 'birth_date' => ['required', 'date_format:Y-m-d', 'before_or_equal:today']
  • '2017-08-27' was parsed as '2017-08-27 22:41:37'.
  • 'today' was parsed as '2017-08-27 00:00:00'.
  • Validation failed.

Some further information I found (thanks to @arjasco 's comment on the previous PR):


@taylorotwell "Isn't this going to break applications? What if people want the time part?"

If you have time fields in the date format, the validation will still use them.
http://php.net/manual/en/datetime.createfromformat.php

If format contains the character !, then portions of the generated time not provided in format, as well as values to the left-hand side of the !, will be set to corresponding values from the Unix epoch.

>>> DateTime::createFromFormat('!Y-m-d H:i:s', '2017-08-28 18:57:24')
=> DateTime {#890
     +"date": "2017-08-28 18:57:24.000000",
     +"timezone_type": 3,
     +"timezone": "Europe/Paris",
   }

I have also added a number of date validation tests in a second commit. These tests deal with the time parts, and show that the fix I made is not breaking anything.

If you still think this change might cause issues, please provide a test case that this PR causes to fail, so that I have a chance of fixing the problem.


The same changes can probably be applied to v5.5 to fix the bug there too, especially with the new date_equals validator which will probably suffer from this issue quite badly.

…today's date and a format is specified

The issue was that the time part was bleeding through from the current timestamp in `DateTime::createFromFormat`.
Test case:
* Validation rule: 'birth_date' => ['required', 'date_format:Y-m-d', 'before_or_equal:today']
* '2017-08-27' was parsed as '2017-08-27 22:41:37'.
* 'today' was parsed as '2017-08-27 00:00:00'.
* Validation failed.
This shows that the fix in 1b651e6 is not going to break applications, people can still use the time part.
This should address the comment of @taylorotwell at laravel#20789 .
@taylorotwell
Copy link
Member

Send it to 5.5 branch please.

@pantherdd
Copy link
Contributor Author

@taylorotwell Done (see here). But is there any reason why the fix can't make it into 5.4 too...? Unless v5.4.36 is the last of that minor 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.

2 participants