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

Reverse DateTimeToStringTransformer must use format #1183

Closed

Conversation

hlecorche
Copy link
Contributor

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.

$last_errors = \DateTime::getLastErrors();
if($last_errors['warning_count'] > 0 || $last_errors['error_count'] > 0){
throw new \Exception('Date is invalid.');
}
Copy link
Contributor

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']));
}

Copy link
Contributor Author

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?

Copy link
Contributor

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 ;-)

@hlecorche
Copy link
Contributor Author

The code is updated. Thanks stloyd :)

@fabpot
Copy link
Member

fabpot commented Jun 7, 2011

Tests do not pass for me after applying this patch:

1) Symfony\Tests\Component\Form\Extension\Core\Type\DateTypeTest::testSetData_differentTimezones
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-01.06.2010
+02.06.2010

@hlecorche
Copy link
Contributor Author

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?

stloyd added a commit to stloyd/symfony that referenced this pull request Jun 30, 2011
* 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
@stloyd
Copy link
Contributor

stloyd commented Jun 30, 2011

@hlecorche Please check PR #1485. Should fix your issue.

@fabpot fabpot closed this in c9a9200 Jul 4, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants