-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Reverse DateTimeToStringTransformer must use format #1183
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
Conversation
$last_errors = \DateTime::getLastErrors(); | ||
if($last_errors['warning_count'] > 0 || $last_errors['error_count'] > 0){ | ||
throw new \Exception('Date is invalid.'); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO better would be something like:
$errors = \DateTime::getLastErrors();
if ($errors['warning_count'] > 0) {
throw new \Exception(current($errors['warning']));
} else if ($errors['error_count'] > 0) {
throw new \Exception(current($errors['error']));
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return only the first warning / error ?
Return only errors (and not warnings) if there are errors and warnings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's proposal ;-) and still is better then you first approach :-)
$errors = \DateTime::getLastErrors();
if ($errors['warning_count'] > 0 || $errors['error_count'] > 0) {
if ($errors['warning_count'] > 0) {
throw new \Exception('Date is invalid. List of warnings: "' . implode(', "', array_values($errors['warning'])) . '"');
}
if ($errors['error_count'] > 0) {
throw new \Exception('Date is invalid. List of errors: "' . implode(', "', array_values($errors['error'])) . '"');
}
}
Remember it's only proposal ;-)
The code is updated. Thanks stloyd :) |
Tests do not pass for me after applying this patch:
|
It's strange because tests pass for me :/ (with last commit 2d91183) : phpunit tests/Symfony/Tests/Component/Form/Extension/Core/Type/DateTypeTest
PHPUnit 3.5.13 by Sebastian Bergmann.
..........IIIIIIIIIIIIIIII...
Time: 1 second, Memory: 6.75Mb
?[30;43m?[2KOK, but incomplete or skipped tests!
?[0m?[30;43m?[2KTests: 29, Assertions: 46, Incomplete: 16.
?[0m?[2K Other persons have this problem? |
* TimeType: - seconds are no longer populated if "with_seconds" = false - "widget = text" is now properly rendered (closes symfony#1480) * DateTimeToStringTransformer: - fixed using not default "format" (probably fix symfony#1183) * DateType, DateTimeType, TimeType: - fixed "input = datetime" and test covered - a bit changed readability
@hlecorche Please check PR #1485. Should fix your issue. |
Continuation of pull request #1134 (I am sorry, the old pull request has been deleted because I deleted one commit which was in a bad branch).
Summary:
Before: In DateTimeToStringTransformer, transform function uses "format" but not "reverseTransform".
If the format is different than "Y-m-d", reverseTransform throws TransformationFailedException.
Now: "reverse" function uses "format". I added "\DateTime::getLastErrors()" because DateTime::createFromFormat doesn't throw exception.
Now, if a bad date (2011-02-29) is given, it is refused.
I added additional tests.