bug #28372 [Form] Fix DateTimeType html5 input format (franzwilding, mcfedr)
This PR was merged into the 2.8 branch. Discussion ---------- [Form] Fix DateTimeType html5 input format | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #27233, #27254 | License | MIT | Doc PR | N/A Fix DateTimeType' HTML input format according to HTML specs. Currently `DateTimeType` produces html with format `yyyy-MM-dd'T'HH:mm:ssZ` but the HTML5 spec expects `yyyy-MM-dd'T'HH:mm:ss` (i.e. no `Z`). Chrome presents an empty date picker meaning edits or having a default date are broken. Also the reverseTransform was expect to have a timezone attached, which it does not - and incorrectly marks it as being a UTC time in this case, instead of using the Transformers output TZ. This is same as @franzwilding https://github.com/symfony/symfony/pull/27254 but with change to just straight use of `DateTime::format` and handling TZ in reverseTransform Commits -------e21a1a4df1
Added relevent links for parsing to the phpdoc4f06f1524d
Add stricter checking for valid date time string253d0a683b
[Form] Fix DateTimeType html5 input format
This commit is contained in:
commit
8d90df7ff7
@ -0,0 +1,104 @@
|
||||
<?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>
|
||||
* @author Fred Cox <mcfedr@gmail.com>
|
||||
*/
|
||||
class DateTimeToHtml5LocalDateTimeTransformer extends BaseDateTimeTransformer
|
||||
{
|
||||
const HTML5_FORMAT = 'Y-m-d\\TH:i:s';
|
||||
|
||||
/**
|
||||
* Transforms a \DateTime into a local date and time string.
|
||||
*
|
||||
* 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.
|
||||
* https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-local-date-and-time-string
|
||||
*
|
||||
* @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 $dateTime->format(self::HTML5_FORMAT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a local date and time string into a \DateTime.
|
||||
*
|
||||
* When transforming back to DateTime the regex is slightly laxer, taking into
|
||||
* account rules for parsing a local date and time string
|
||||
* https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#parse-a-local-date-and-time-string
|
||||
*
|
||||
* @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 (!preg_match('/^(\d{4})-(\d{2})-(\d{2})[T ]\d{2}:\d{2}(?::\d{2})?$/', $dateTimeLocal, $matches)) {
|
||||
throw new TransformationFailedException(sprintf('The date "%s" is not a valid date.', $dateTimeLocal));
|
||||
}
|
||||
|
||||
try {
|
||||
$dateTime = new \DateTime($dateTimeLocal, new \DateTimeZone($this->outputTimezone));
|
||||
} catch (\Exception $e) {
|
||||
throw new TransformationFailedException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
|
||||
if ($this->inputTimezone !== $dateTime->getTimezone()->getName()) {
|
||||
$dateTime->setTimezone(new \DateTimeZone($this->inputTimezone));
|
||||
}
|
||||
|
||||
if (!checkdate($matches[2], $matches[3], $matches[1])) {
|
||||
throw new TransformationFailedException(sprintf('The date "%s-%s-%s" is not a valid date.', $matches[1], $matches[2], $matches[3]));
|
||||
}
|
||||
|
||||
return $dateTime;
|
||||
}
|
||||
}
|
@ -15,8 +15,8 @@ use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\DataTransformer\ArrayToPartsTransformer;
|
||||
use Symfony\Component\Form\Extension\Core\DataTransformer\DataTransformerChain;
|
||||
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToArrayTransformer;
|
||||
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToHtml5LocalDateTimeTransformer;
|
||||
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer;
|
||||
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToRfc3339Transformer;
|
||||
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
|
||||
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
@ -33,21 +33,8 @@ class DateTimeType extends AbstractType
|
||||
const DEFAULT_TIME_FORMAT = \IntlDateFormatter::MEDIUM;
|
||||
|
||||
/**
|
||||
* This is not quite the HTML5 format yet, because ICU lacks the
|
||||
* capability of parsing and generating RFC 3339 dates.
|
||||
*
|
||||
* For more information see:
|
||||
*
|
||||
* http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax
|
||||
* https://www.w3.org/TR/html5/sec-forms.html#local-date-and-time-state-typedatetimelocal
|
||||
* http://tools.ietf.org/html/rfc3339
|
||||
*
|
||||
* An ICU ticket was created:
|
||||
* http://icu-project.org/trac/ticket/9421
|
||||
*
|
||||
* It was supposedly fixed, but is not available in all PHP installations
|
||||
* yet. To temporarily circumvent this issue, DateTimeToRfc3339Transformer
|
||||
* is used when the format matches this constant.
|
||||
* The HTML5 datetime-local format as defined in
|
||||
* http://w3c.github.io/html-reference/datatypes.html#form.data.datetime-local.
|
||||
*/
|
||||
const HTML5_FORMAT = "yyyy-MM-dd'T'HH:mm:ss";
|
||||
|
||||
@ -88,7 +75,7 @@ class DateTimeType extends AbstractType
|
||||
|
||||
if ('single_text' === $options['widget']) {
|
||||
if (self::HTML5_FORMAT === $pattern) {
|
||||
$builder->addViewTransformer(new DateTimeToRfc3339Transformer(
|
||||
$builder->addViewTransformer(new DateTimeToHtml5LocalDateTimeTransformer(
|
||||
$options['model_timezone'],
|
||||
$options['view_timezone']
|
||||
));
|
||||
|
@ -1603,7 +1603,7 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
|
||||
[@type="datetime-local"]
|
||||
[@name="name"]
|
||||
[@class="my&class form-control"]
|
||||
[@value="2011-02-03T04:05:06Z"]
|
||||
[@value="2011-02-03T04:05:06"]
|
||||
'
|
||||
);
|
||||
}
|
||||
@ -1624,7 +1624,7 @@ abstract class AbstractBootstrap3LayoutTest extends AbstractLayoutTest
|
||||
[@type="datetime-local"]
|
||||
[@name="name"]
|
||||
[@class="my&class form-control"]
|
||||
[@value="2011-02-03T04:05:06Z"]
|
||||
[@value="2011-02-03T04:05:06"]
|
||||
'
|
||||
);
|
||||
}
|
||||
|
@ -1502,7 +1502,7 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
|
||||
'/input
|
||||
[@type="datetime-local"]
|
||||
[@name="name"]
|
||||
[@value="2011-02-03T04:05:06Z"]
|
||||
[@value="2011-02-03T04:05:06"]
|
||||
'
|
||||
);
|
||||
}
|
||||
@ -1522,7 +1522,7 @@ abstract class AbstractLayoutTest extends FormIntegrationTestCase
|
||||
'/input
|
||||
[@type="datetime-local"]
|
||||
[@name="name"]
|
||||
[@value="2011-02-03T04:05:06Z"]
|
||||
[@value="2011-02-03T04:05:06"]
|
||||
'
|
||||
);
|
||||
}
|
||||
|
@ -0,0 +1,129 @@
|
||||
<?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\DateTimeToHtml5LocalDateTimeTransformer;
|
||||
|
||||
class DateTimeToHtml5LocalDateTimeTransformerTest extends TestCase
|
||||
{
|
||||
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 transformProvider()
|
||||
{
|
||||
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'),
|
||||
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'),
|
||||
array('America/New_York', 'UTC', '2010-02-03 04:05:06 America/New_York', '2010-02-03T09:05:06'),
|
||||
);
|
||||
}
|
||||
|
||||
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'),
|
||||
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'),
|
||||
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'),
|
||||
array('Europe/Amsterdam', 'Europe/Amsterdam', '2013-08-21 10:30:00 Europe/Amsterdam', '2013-08-21T10:30:00'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider transformProvider
|
||||
*/
|
||||
public function testTransform($fromTz, $toTz, $from, $to)
|
||||
{
|
||||
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($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 DateTimeToHtml5LocalDateTimeTransformer($fromTz, $toTz);
|
||||
|
||||
$this->assertSame($to, $transformer->transform(null !== $from ? new \DateTimeImmutable($from) : null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testTransformRequiresValidDateTime()
|
||||
{
|
||||
$transformer = new DateTimeToHtml5LocalDateTimeTransformer();
|
||||
$transformer->transform('2010-01-01');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider reverseTransformProvider
|
||||
*/
|
||||
public function testReverseTransform($toTz, $fromTz, $to, $from)
|
||||
{
|
||||
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($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 DateTimeToHtml5LocalDateTimeTransformer();
|
||||
$transformer->reverseTransform(12345);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testReverseTransformWithNonExistingDate()
|
||||
{
|
||||
$transformer = new DateTimeToHtml5LocalDateTimeTransformer('UTC', 'UTC');
|
||||
|
||||
$transformer->reverseTransform('2010-04-31T04:05');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testReverseTransformExpectsValidDateString()
|
||||
{
|
||||
$transformer = new DateTimeToHtml5LocalDateTimeTransformer('UTC', 'UTC');
|
||||
|
||||
$transformer->reverseTransform('2010-2010-2010');
|
||||
}
|
||||
}
|
@ -221,12 +221,12 @@ class DateTimeTypeTest extends BaseTypeTest
|
||||
|
||||
$outputTime = new \DateTime('2010-06-02 03:04:00 Pacific/Tahiti');
|
||||
|
||||
$form->submit('2010-06-02T03:04:00-10:00');
|
||||
$form->submit('2010-06-02T03:04:00');
|
||||
|
||||
$outputTime->setTimezone(new \DateTimeZone('America/New_York'));
|
||||
|
||||
$this->assertEquals($outputTime, $form->getData());
|
||||
$this->assertEquals('2010-06-02T03:04:00-10:00', $form->getViewData());
|
||||
$this->assertEquals('2010-06-02T03:04:00', $form->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitStringSingleText()
|
||||
@ -238,10 +238,10 @@ class DateTimeTypeTest extends BaseTypeTest
|
||||
'widget' => 'single_text',
|
||||
));
|
||||
|
||||
$form->submit('2010-06-02T03:04:00Z');
|
||||
$form->submit('2010-06-02T03:04:00');
|
||||
|
||||
$this->assertEquals('2010-06-02 03:04:00', $form->getData());
|
||||
$this->assertEquals('2010-06-02T03:04:00Z', $form->getViewData());
|
||||
$this->assertEquals('2010-06-02T03:04:00', $form->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitStringSingleTextWithSeconds()
|
||||
@ -254,10 +254,10 @@ class DateTimeTypeTest extends BaseTypeTest
|
||||
'with_seconds' => true,
|
||||
));
|
||||
|
||||
$form->submit('2010-06-02T03:04:05Z');
|
||||
$form->submit('2010-06-02T03:04:05');
|
||||
|
||||
$this->assertEquals('2010-06-02 03:04:05', $form->getData());
|
||||
$this->assertEquals('2010-06-02T03:04:05Z', $form->getViewData());
|
||||
$this->assertEquals('2010-06-02T03:04:05', $form->getViewData());
|
||||
}
|
||||
|
||||
public function testSubmitDifferentPattern()
|
||||
|
Reference in New Issue
Block a user