-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Fix DateTimeType html5 input format. #27254
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
...Component/Form/Extension/Core/DataTransformer/DateTimeToHtml5DateTimeLocalTransformer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\Core\DataTransformer; | ||
|
||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
/** | ||
* @author Franz Wilding <franz.wilding@me.com> | ||
* @author Bernhard Schussek <bschussek@gmail.com> | ||
*/ | ||
class DateTimeToHtml5DateTimeLocalTransformer extends BaseDateTimeTransformer | ||
{ | ||
/** | ||
* Transforms a normalized date into a localized date without trailing timezone. | ||
* | ||
* According to the HTML standard, the input string of a datetime-local | ||
* input is a RFC3339 date followed by 'T', followed by a RFC3339 time. | ||
* http://w3c.github.io/html-reference/datatypes.html#form.data.datetime-local | ||
* | ||
* @param \DateTime|\DateTimeInterface $dateTime A DateTime object | ||
* | ||
* @return string The formatted date | ||
* | ||
* @throws TransformationFailedException If the given value is not an | ||
* instance of \DateTime or \DateTimeInterface | ||
*/ | ||
public function transform($dateTime) | ||
{ | ||
if (null === $dateTime) { | ||
return ''; | ||
} | ||
|
||
if (!$dateTime instanceof \DateTime && !$dateTime instanceof \DateTimeInterface) { | ||
throw new TransformationFailedException('Expected a \DateTime or \DateTimeInterface.'); | ||
} | ||
|
||
if ($this->inputTimezone !== $this->outputTimezone) { | ||
if (!$dateTime instanceof \DateTimeImmutable) { | ||
$dateTime = clone $dateTime; | ||
} | ||
|
||
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone)); | ||
} | ||
|
||
return preg_replace('/\+00:00$/', '', $dateTime->format('c')); | ||
} | ||
|
||
/** | ||
* Transforms a formatted datetime-local string into a normalized date. | ||
* | ||
* @param string $dateTimeLocal Formatted string | ||
* | ||
* @return \DateTime Normalized date | ||
* | ||
* @throws TransformationFailedException If the given value is not a string, | ||
* if the value could not be transformed | ||
*/ | ||
public function reverseTransform($dateTimeLocal) | ||
{ | ||
if (!\is_string($dateTimeLocal)) { | ||
throw new TransformationFailedException('Expected a string.'); | ||
} | ||
|
||
if ('' === $dateTimeLocal) { | ||
return; | ||
} | ||
|
||
if ('Z' !== substr($dateTimeLocal, -1)) { | ||
$dateTimeLocal .= 'Z'; | ||
} | ||
|
||
try { | ||
$dateTime = new \DateTime($dateTimeLocal); | ||
} catch (\Exception $e) { | ||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e); | ||
} | ||
|
||
if ($this->inputTimezone !== $dateTime->getTimezone()->getName()) { | ||
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone)); | ||
} | ||
|
||
if (preg_match('/(\d{4})-(\d{2})-(\d{2})/', $dateTimeLocal, $m)) { | ||
if (!checkdate($m[2], $m[3], $m[1])) { | ||
throw new TransformationFailedException(sprintf('The date "%s-%s-%s" is not a valid date.', $m[1], $m[2], $m[3])); | ||
} | ||
} | ||
|
||
return $dateTime; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
...orm/Tests/Extension/Core/DataTransformer/DateTimeToHtml5DateTimeLocaleTransformerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToHtml5DateTimeLocalTransformer; | ||
|
||
class DateTimeToHtml5DateTimeLocaleTransformerTest extends TestCase | ||
{ | ||
protected $dateTime; | ||
protected $dateTimeWithoutSeconds; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
$this->dateTime = new \DateTime('2010-02-03 04:05:06 UTC'); | ||
$this->dateTimeWithoutSeconds = new \DateTime('2010-02-03 04:05:00 UTC'); | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
$this->dateTime = null; | ||
$this->dateTimeWithoutSeconds = null; | ||
} | ||
|
||
public static function assertEquals($expected, $actual, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false) | ||
{ | ||
if ($expected instanceof \DateTime && $actual instanceof \DateTime) { | ||
$expected = $expected->format('c'); | ||
$actual = $actual->format('c'); | ||
} | ||
|
||
parent::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase); | ||
} | ||
|
||
public function allProvider() | ||
{ | ||
return array( | ||
array('UTC', 'UTC', '2010-02-03 04:05:06 UTC', '2010-02-03T04:05:06'), | ||
array('UTC', 'UTC', null, ''), | ||
array('America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:06 America/New_York', '2010-02-03T17:05:06+08:00'), | ||
array('America/New_York', 'Asia/Hong_Kong', null, ''), | ||
array('UTC', 'Asia/Hong_Kong', '2010-02-03 04:05:06 UTC', '2010-02-03T12:05:06+08:00'), | ||
array('America/New_York', 'UTC', '2010-02-03 04:05:06 America/New_York', '2010-02-03T09:05:06'), | ||
); | ||
} | ||
|
||
public function transformProvider() | ||
{ | ||
return $this->allProvider(); | ||
} | ||
|
||
public function reverseTransformProvider() | ||
{ | ||
return array( | ||
// format without seconds, as appears in some browsers | ||
array('UTC', 'UTC', '2010-02-03 04:05:06 UTC', '2010-02-03T04:05:06'), | ||
array('UTC', 'UTC', null, ''), | ||
array('America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:06 America/New_York', '2010-02-03T17:05:06+08:00'), | ||
array('America/New_York', 'Asia/Hong_Kong', null, ''), | ||
array('UTC', 'Asia/Hong_Kong', '2010-02-03 04:05:06 UTC', '2010-02-03T12:05:06+08:00'), | ||
array('America/New_York', 'UTC', '2010-02-03 04:05:06 America/New_York', '2010-02-03T09:05:06'), | ||
array('UTC', 'UTC', '2010-02-03 04:05:00 UTC', '2010-02-03T04:05'), | ||
array('America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:00 America/New_York', '2010-02-03T17:05+08:00'), | ||
array('Europe/Amsterdam', 'Europe/Amsterdam', '2013-08-21 10:30:00 Europe/Amsterdam', '2013-08-21T08:30:00'), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider transformProvider | ||
*/ | ||
public function testTransform($fromTz, $toTz, $from, $to) | ||
{ | ||
$transformer = new DateTimeToHtml5DateTimeLocalTransformer($fromTz, $toTz); | ||
|
||
$this->assertSame($to, $transformer->transform(null !== $from ? new \DateTime($from) : null)); | ||
} | ||
|
||
/** | ||
* @dataProvider transformProvider | ||
* @requires PHP 5.5 | ||
*/ | ||
public function testTransformDateTimeImmutable($fromTz, $toTz, $from, $to) | ||
{ | ||
$transformer = new DateTimeToHtml5DateTimeLocalTransformer($fromTz, $toTz); | ||
|
||
$this->assertSame($to, $transformer->transform(null !== $from ? new \DateTimeImmutable($from) : null)); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testTransformRequiresValidDateTime() | ||
{ | ||
$transformer = new DateTimeToHtml5DateTimeLocalTransformer(); | ||
$transformer->transform('2010-01-01'); | ||
} | ||
|
||
/** | ||
* @dataProvider reverseTransformProvider | ||
*/ | ||
public function testReverseTransform($toTz, $fromTz, $to, $from) | ||
{ | ||
$transformer = new DateTimeToHtml5DateTimeLocalTransformer($toTz, $fromTz); | ||
|
||
if (null !== $to) { | ||
$this->assertEquals(new \DateTime($to), $transformer->reverseTransform($from)); | ||
} else { | ||
$this->assertNull($transformer->reverseTransform($from)); | ||
} | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testReverseTransformRequiresString() | ||
{ | ||
$transformer = new DateTimeToHtml5DateTimeLocalTransformer(); | ||
$transformer->reverseTransform(12345); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testReverseTransformWithNonExistingDate() | ||
{ | ||
$transformer = new DateTimeToHtml5DateTimeLocalTransformer('UTC', 'UTC'); | ||
|
||
$transformer->reverseTransform('2010-04-31T04:05Z'); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException | ||
*/ | ||
public function testReverseTransformExpectsValidDateString() | ||
{ | ||
$transformer = new DateTimeToHtml5DateTimeLocalTransformer('UTC', 'UTC'); | ||
|
||
$transformer->reverseTransform('2010-2010-2010'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Would it not make more sense to do
->format(DateTimeType::HTML5_FORMAT)
rather thanpreg_replace
ing format(c)?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.
Of course not, because wrong time of format, but maybe having a different
const
here with"Y-m-d\TH:i:s"
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.
This was taken from
symfony/src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToRfc3339Transformer.php
Line 48 in 5129c4c
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.
I see that, but seems like it would be more efficient, and obvious what is happening, to pass to
format
the format you actually want, rather than the wrong format, and editing it with regex.