10000 feature #50059 [Form] Don't render seconds for HTML5 date pickers unl… · symfony/symfony@6378563 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6378563

Browse files
feature #50059 [Form] Don't render seconds for HTML5 date pickers unless "with_seconds" is explicitly set (fancyweb)
This PR was merged into the 6.3 branch. Discussion ---------- [Form] Don't render seconds for HTML5 date pickers unless "with_seconds" is explicitly set | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | yes | Deprecations? | no | Tickets | Fix #48076 | License | MIT | Doc PR | - Alternative to #49158 (commit credits due to `@fancyweb`) I don't think we need a deprecation for this change. To me, it's a bugfix. But merging it on 6.3 might be safer so let's do it as a new feature. Commits ------- 93008ee [Form] Don't render seconds for HTML5 date pickers unless "with_seconds" is explicitly set
2 parents bf8617d + 93008ee commit 6378563

File tree

10 files changed

+46
-26
lines changed

10 files changed

+46
-26
lines changed

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,7 @@ public function testDateTimeWithWidgetSingleText()
17531753
[@type="datetime-local"]
17541754
[@name="name"]
17551755
[@class="my&class form-control"]
1756-
[@value="2011-02-03T04:05:06"]
1756+
[@value="2011-02-03T04:05"]
17571757
'
17581758
);
17591759
}

src/Symfony/Bridge/Twig/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"symfony/asset": "^5.4|^6.0",
2929
"symfony/dependency-injection": "^5.4|^6.0",
3030
"symfony/finder": "^5.4|^6.0",
31-
"symfony/form": "^6.2.7",
31+
"symfony/form": "^6.3",
3232
"symfony/html-sanitizer": "^6.1",
3333
"symfony/http-foundation": "^5.4|^6.0",
3434
"symfony/http-kernel": "^6.2",
@@ -57,7 +57,7 @@
5757
"phpdocumentor/reflection-docblock": "<3.2.2",
5858
"phpdocumentor/type-resolver": "<1.4.0",
5959
"symfony/console": "<5.4",
60-
"symfony/form": "<6.2.7",
60+
"symfony/form": "<6.3",
6161
"symfony/http-foundation": "<5.4",
6262
"symfony/http-kernel": "<6.2",
6363
"symfony/mime": "<6.2",

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
6.3
55
---
66

7+
* Don't render seconds for HTML5 date pickers unless "with_seconds" is explicitly set
78
* Add a `placeholder_attr` option to `ChoiceType`
89

910
6.2

src/Symfony/Component/Form/Extension/Core/DataTransformer/DateTimeToHtml5LocalDateTimeTransformer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
class DateTimeToHtml5LocalDateTimeTransformer extends BaseDateTimeTransformer
2424
{
2525
public const HTML5_FORMAT = 'Y-m-d\\TH:i:s';
26+
public const HTML5_FORMAT_NO_SECONDS = 'Y-m-d\\TH:i';
27+
28+
public function __construct(string $inputTimezone = null, string $outputTimezone = null, private bool $withSeconds = false)
29+
{
30+
parent::__construct($inputTimezone, $outputTimezone);
31+
}
2632

2733
/**
2834
* Transforms a \DateTime into a local date and time string.
@@ -54,7 +60,7 @@ public function transform(mixed $dateTime): string
5460
$dateTime = $dateTime->setTimezone(new \DateTimeZone($this->outputTimezone));
5561
}
5662

57-
return $dateTime->format(self::HTML5_FORMAT);
63+
return $dateTime->format($this->withSeconds ? self::HTML5_FORMAT : self::HTML5_FORMAT_NO_SECONDS);
5864
}
5965

6066
/**

src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7979
if (self::HTML5_FORMAT === $pattern) {
8080
$builder->addViewTransformer(new DateTimeToHtml5LocalDateTimeTransformer(
8181
$options['model_timezone'],
82-
$options['view_timezone']
82+
$options['view_timezone'],
83+
$options['with_seconds']
8384
));
8485
} else {
8586
$builder->addViewTransformer(new DateTimeToLocalizedStringTransformer(
@@ -218,8 +219,12 @@ public function buildView(FormView $view, FormInterface $form, array $options)
218219
// adding the HTML attribute step if not already defined.
219220
// Otherwise the browser will not display and so not send the seconds
220221
// therefore the value will always be considered as invalid.
221-
if ($options['with_seconds'] && !isset($view->vars['attr']['step'])) {
222-
$view->vars['attr']['step'] = 1;
222+
if (!isset($view->vars['attr']['step'])) {
223+
if ($options['with_seconds']) {
224+
$view->vars['attr']['step'] = 1;
225+
} elseif (!$options['with_minutes']) {
226+
$view->vars['attr']['step'] = 3600;
227+
}
223228
}
224229
}
225230
}

src/Symfony/Component/Form/Extension/Core/Type/TimeType.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,12 @@ public function buildView(FormView $view, FormInterface $form, array $options)
231231
// adding the HTML attribute step if not already defined.
232232
// Otherwise the browser will not display and so not send the seconds
233233
// therefore the value will always be considered as invalid.
234-
if ($options['with_seconds'] && !isset($view->vars['attr']['step'])) {
235-
$view->vars['attr']['step'] = 1;
234+
if (!isset($view->vars['attr']['step'])) {
235+
if ($options['with_seconds']) {
236+
$view->vars['attr']['step'] = 1;
237+
} elseif (!$options['with_minutes']) {
238+
$view->vars['attr']['step'] = 3600;
239+
}
236240
}
237241
}
238242
}

src/Symfony/Component/Form/Tests/AbstractLayoutTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ public function testDateTimeWithWidgetSingleText()
15231523
'/input
15241524
[@type="datetime-local"]
15251525
[@name="name"]
1526-
[@value="2011-02-03T04:05:06"]
1526+
[@value="2011-02-03T04:05"]
15271527
'
15281528
);
15291529
}

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToHtml5LocalDateTimeTransformerTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ class DateTimeToHtml5LocalDateTimeTransformerTest extends BaseDateTimeTransforme
2323
public static function transformProvider()
2424
{
2525
return [
26-
['UTC', 'UTC', '2010-02-03 04:05:06 UTC', '2010-02-03T04:05:06'],
27-
['UTC', 'UTC', null, ''],
28-
['America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:06 America/New_York', '2010-02-03T17:05:06'],
29-
['America/New_York', 'Asia/Hong_Kong', null, ''],
30-
['UTC', 'Asia/Hong_Kong', '2010-02-03 04:05:06 UTC', '2010-02-03T12:05:06'],
31-
['America/New_York', 'UTC', '2010-02-03 04:05:06 America/New_York', '2010-02-03T09:05:06'],
26+
['UTC', 'UTC', '2010-02-03 04:05:06 UTC', '2010-02-03T04:05:06', true],
27+
['UTC', 'UTC', null, '', true],
28+
['America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:06 America/New_York', '2010-02-03T17:05:06', true],
29+
['America/New_York', 'Asia/Hong_Kong', null, '', true],
30+
['UTC', 'Asia/Hong_Kong', '2010-02-03 04:05:06 UTC', '2010-02-03T12:05:06', true],
31+
['America/New_York', 'UTC', '2010-02-03 04:05:06 America/New_York', '2010-02-03T09:05:06', true],
32+
['UTC', 'UTC', '2010-02-03 04:05:06 UTC', '2010-02-03T04:05', false],
33+
['America/New_York', 'Asia/Hong_Kong', '2010-02-03 04:05:06 America/New_York', '2010-02-03T17:05', false],
34+
['UTC', 'Asia/Hong_Kong', '2010-02-03 04:05:06 UTC', '2010-02-03T12:05', false],
35+
['America/New_York', 'UTC', '2010-02-03 04:05:06 America/New_York', '2010-02-03T09:05', false],
3236
];
3337
}
3438

@@ -55,19 +59,19 @@ public static function reverseTransformProvider()
5559
/**
5660
* @dataProvider transformProvider
5761
*/
58-
public function testTransform($fromTz, $toTz, $from, $to)
62+
public function testTransform($fromTz, $toTz, $from, $to, bool $withSeconds)
5963
{
60-
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($fromTz, $toTz);
64+
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($fromTz, $toTz, $withSeconds);
6165

6266
$this->assertSame($to, $transformer->transform(null !== $from ? new \DateTime($from) : null));
6367
}
6468

6569
/**
6670
* @dataProvider transformProvider
6771
*/
68-
public function testTransformDateTimeImmutable($fromTz, $toTz, $from, $to)
72+
public function testTransformDateTimeImmutable($fromTz, $toTz, $from, $to, bool $withSeconds)
6973
{
70-
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($fromTz, $toTz);
74+
$transformer = new DateTimeToHtml5LocalDateTimeTransformer($fromTz, $toTz, $withSeconds);
7175

7276
$this->assertSame($to, $transformer->transform(null !== $from ? new \DateTimeImmutable($from) : null));
7377
}

src/Symfony/Component/Form/Tests/Extension/Core/Type/DateTimeTypeTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public function testSubmitDifferentTimezonesDateTime()
252252
$outputTime->setTimezone(new \DateTimeZone('America/New_York'));
253253

254254
$this->assertEquals($outputTime, $form->getData());
255-
$this->assertEquals('2010-06-02T03:04:00', $form->getViewData());
255+
$this->assertEquals('2010-06-02T03:04', $form->getViewData());
256256
}
257257

258258
public function testSubmitDifferentTimezonesDateTimeImmutable()
@@ -272,7 +272,7 @@ public function testSubmitDifferentTimezonesDateTimeImmutable()
272272

273273
$this->assertInstanceOf(\DateTimeImmutable::class, $form->getData());
274274
$this->assertEquals($outputTime, $form->getData());
275-
$this->assertEquals('2010-06-02T03:04:00', $form->getViewData());
275+
$this->assertEquals('2010-06-02T03:04', $form->getViewData());
276276
}
277277

278278
public function testSubmitStringSingleText()
@@ -287,7 +287,7 @@ public function testSubmitStringSingleText()
287287
$form->submit('2010-06-02T03:04:00');
288288

289289
$this->assertEquals('2010-06-02 03:04:00', $form->getData());
290-
$this->assertEquals('2010-06-02T03:04:00', $form->getViewData());
290+
$this->assertEquals('2010-06-02T03:04', $form->getViewData());
291291
}
292292

293293
public function testSubmitStringSingleTextWithSeconds()
@@ -709,10 +709,10 @@ public function testSubmitNullUsesDateEmptyData($widget, $emptyData, $expectedDa
709709
public static function provideEmptyData()
710710
{
711711
$expectedData = \DateTime::createFromFormat('Y-m-d H:i', '2018-11-11 21:23');
712-
$lazyEmptyData = static fn (FormInterface $form) => $form->getConfig()->getCompound() ? ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']] : '2018-11-11T21:23:00';
712+
$lazyEmptyData = static fn (FormInterface $form) => $form->getConfig()->getCompound() ? ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']] : '2018-11-11T21:23';
713713

714714
return [
715-
'Simple field' => ['single_text', '2018-11-11T21:23:00', $expectedData],
715+
'Simple field' => ['single_text', '2018-11-11T21:23', $expectedData],
716716
'Compound text field' => ['text', ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']], $expectedData],
717717
'Compound choice field' => ['choice', ['date' => ['year' => '2018', 'month' => '11', 'day' => '11'], 'time' => ['hour' => '21', 'minute' => '23']], $expectedData],
718718
'Simple field lazy' => ['single_text', $lazyEmptyData, $expectedData],

src/Symfony/Component/Form/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"symfony/http-kernel": "<5.4",
5454
"symfony/translation": "<5.4",
5555
"symfony/translation-contracts": "<2.5",
56-
"symfony/twig-bridge": "<5.4.21|>=6,<6.2.7"
56+
"symfony/twig-bridge": "<6.3"
5757
},
5858
"suggest": {
5959
"symfony/validator": "For form validation.",

0 commit comments

Comments
 (0)
0