-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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] Validation Rule: date_format bug #17642
Comments
I'm sure this is a PHP bug. Any months with 31 days will format correctly. I'm not a PHP internals expert here but It appears then when you define just the month it's using the last possible day out of all months (31) and therefore overflowing into the next month because e.g feb only has 28 days. I wonder if we could do something like this that appears in Carbon |
Hi, I tried running some quick tests on the validation rule 'date_format' See this line here It seems that you are right, it is a quirky behavior of the DateTime::createFromFormat method I tested it using the input value '2017-02' A quick call on
Next, I tried this
Finally,
The return value looks like this Translating it with the values
So it seems it is not a Laravel related bug. The suggestion you raised above on preparing a solution to handle overflows that is similar with the Carbon seems good. |
Basically just change return ['testDate' => ['bail', required, 'date_format:Y-m']]; to return ['testDate' => ['bail', required, 'date_format:!Y-m']]; Should work... |
@fernandobandeira thanks :) |
@MiSAKACHi please check the latest comments for a fix. |
@themsaid @MiSAKACHi @fernandobandeira Fun fact: As it is now 1st February this problem has now disappeared 😉 php -r "echo DateTime::createFromFormat('Y-m', '2016-02')->format('Y-m') . \"\n\";" Roll your clock back to yesterday and run the above again.. hehe. |
The rule
Yes, it so weird 👍 ... I temporarily used the validation rule 'date' & limited the max length to 7 to resolve the issue. |
@MiSAKACHi Ahhh. Yes the validation will still fail because the same format is being used for the next comparison. hmm. Glad you found a work around though. Still not correct behaviour, couldn't find anything on the PHP bugs site either. |
Can we re-open this issue? Since this one has no clear solution and the proposed workaround isn't working when I tested it. |
@MiSAKACHi I've left a comment in my pull request to Taylor. My commit does fix the problem using the '!' internally, it's when passing it through the validation instance with Very minor change but because of the inconsistency with it not working depending on what the current month it would be nice to be fixed. It means one day as far as you're concerned your production app works, next day it's potentially not validating correctly anymore. Anyone unaware of the date time issue could spend ages trying to figure out this problem because their code is correct.. |
I'm doing it client side for now:
|
I have the same problem with the date_format February |
Why is this closed if it's still the issue? @themsaid Issue with But dd(validator([
"date" => "2017-02",
], [
"date" => "date_format:!Y-m"
])->passes()); // false |
@decadence My quick one liner here (#17693) as reference above did fix this, but didn't progress any further. Reseting the day to be the 1st if no day is present is another fix. Discussion is also taking place on the Carbon library. |
Just ran into this myself when trying to use For those interested, I ended up just using the |
I seem to be having a similar problem. Today is July 31, 2017 and am using date_format:m to validate the month dropdown of a birthday input. At this moment, if I enter February, April, June, September or November, validation fails and returns: "Invalid month entered for your birthday." Any entry for the months that have 31 days - January, March, May, July, August, October and December - validates with no problem. Likely will create a custom validation rule as a workaround for the time being. |
It still fails when you have |
@hopeseekr raise another issue because Laravel team don't look on closed ones. |
@decadence a PR is better because we're basically trying to trick PHP into this, opening a new issue won't help, but if you have a solution that's much better. @hopeseekr if I were you I'd just do |
Same problem here, UPDATE: I THINK that one solution is: /Illuminate/Validation/Concerns/ValidatesAttributes.php:361 Change 361 line from:
To:
|
This seems fixed on the 5.5 branch |
As the title says, I may be missing something. The scenario is provided below.
Versions used
Problem
Test Data
Working on the following dates [Validation is successful]
Not working on the following dates [Validation does not match the format Y-m]
Steps to Reproduce
Create a request with the following rule
Next steps
Other notes
The text was updated successfully, but these errors were encountered: