diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php index cbaac96859..f7c4eb4798 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToStringTransformer.php @@ -140,6 +140,17 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer $outputTz = new \DateTimeZone($this->outputTimezone); $dateTime = \DateTime::createFromFormat($this->parseFormat, $value, $outputTz); + $lastErrors = \DateTime::getLastErrors(); + + if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) { + throw new TransformationFailedException( + implode(', ', array_merge( + array_values($lastErrors['warnings']), + array_values($lastErrors['errors']) + )) + ); + } + // On PHP versions < 5.3.8 we need to emulate the pipe operator // and reset parts not given in the format to their equivalent // of the UNIX base timestamp. @@ -204,17 +215,6 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer } } - $lastErrors = \DateTime::getLastErrors(); - - if (0 < $lastErrors['warning_count'] || 0 < $lastErrors['error_count']) { - throw new TransformationFailedException( - implode(', ', array_merge( - array_values($lastErrors['warnings']), - array_values($lastErrors['errors']) - )) - ); - } - if ($this->inputTimezone !== $this->outputTimezone) { $dateTime->setTimeZone(new \DateTimeZone($this->inputTimezone)); } diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index 1f296a81df..aa3d46bf2e 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -876,8 +876,10 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase public function testDateTimeWithEmptyValueOnTime() { - $form = $this->factory->createNamed('name', 'datetime', '2011-02-03', array( - 'input' => 'string', + $data = array('year' => '2011', 'month' => '2', 'day' => '3', 'hour' => '', 'minute' => ''); + + $form = $this->factory->createNamed('name', 'datetime', $data, array( + 'input' => 'array', 'empty_value' => array('hour' => 'Change&Me', 'minute' => 'Change&Me'), 'required' => false, ));