8000 fix populating single widget time view data with different timezones · symfony/symfony@44d65de · GitHub
[go: up one dir, main page]

Skip to content

Commit 44d65de

Browse files
committed
fix populating single widget time view data with different timezones
1 parent 01a8e72 commit 44d65de

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7373
}
7474
});
7575

76+
$parseFormat = null;
77+
7678
if (null !== $options['reference_date']) {
77-
$format = 'Y-m-d '.$format;
79+
$parseFormat = 'Y-m-d '.$format;
7880

7981
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
8082
$data = $event->getData();
@@ -85,7 +87,15 @@ public function buildForm(FormBuilderInterface $builder, array $options)
8587
});
8688
}
8789

88-
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
90+
$viewTransformer = new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format);
91+
92+
if (null !== $parseFormat) {
93+
$property = new \ReflectionProperty($viewTransformer, 'parseFormat');
94+
$property->setAccessible(true);
95+
$property->setValue($viewTransformer, $parseFormat);
96+
}
97+
98+
$builder->addViewTransformer($viewTransformer);
8999
} else {
90100
$hourOptions = $minuteOptions = $secondOptions = [
91101
'error_bubbling' => true,

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,78 @@ public function testSubmitWithSecondsAndBrowserOmissionSeconds()
279279
$this->assertEquals('03:04:00', $form->getViewData());
280280
}
281281

282+
public function testPreSetDataDifferentTimezones()
283+
{
284+
var_dump(date_default_timezone_get());
285+
286+
$form = $this->factory->create(static::TESTED_TYPE, null, [
287+
'model_timezone' => 'UTC',
288+
'view_timezone' => 'Europe/Berlin',
289+
'input' => 'datetime',
290+
'with_seconds' => true,
291+
'reference_date' => new \DateTimeImmutable('2019-01-01', new \DateTimeZone('UTC')),
292+
]);
293+
$form->setData(new \DateTime('2022-01-01 15:09:10'));
294+
295+
$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
296+
$this->assertSame([
297+
'hour' => '16',
298+
'minute' => '9',
299+
'second' => '10',
300+
], $form->getViewData());
301+
}
302+
303+
public function testPreSetDataDifferentTimezonesDuringDaylightSavingTime()
304+
{
305+
$form = $this->factory->create(static::TESTED_TYPE, null, [
306+
'model_timezone' => 'UTC',
307+
'view_timezone' => 'Europe/Berlin',
308+
'input' => 'datetime',
309+
'with_seconds' => true,
310+
'reference_date' => new \DateTimeImmutable('2019-07-12', new \DateTimeZone('UTC')),
311+
]);
312+
$form->setData(new \DateTime('2022-04-29 15:09:10'));
313+
314+
$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
315+
$this->assertSame([
316+
'hour' => '17',
317+
'minute' => '9',
318+
'second' => '10',
319+
], $form->getViewData());
320+
}
321+
322+
public function testPreSetDataDifferentTimezonesUsingSingleTextWidget()
323+
{
324+
$form = $this->factory->create(static::TESTED_TYPE, null, [
325+
'model_timezone' => 'UTC',
326+
'view_timezone' => 'Europe/Berlin',
327+
629A 'input' => 'datetime',
328+
'with_seconds' => true,
329+
'reference_date' => new \DateTimeImmutable('2019-01-01', new \DateTimeZone('UTC')),
330+
'widget' => 'single_text',
331+
]);
332+
$form->setData(new \DateTime('2022-01-01 15:09:10'));
333+
334+
$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
335+
$this->assertSame('16:09:10', $form->getViewData());
336+
}
337+
338+
public function testPreSetDataDifferentTimezonesDuringDaylightSavingTimeUsingSingleTextWidget()
339+
{
340+
$form = $this->factory->create(static::TESTED_TYPE, null, [
341+
'model_timezone' => 'UTC',
342+
'view_timezone' => 'Europe/Berlin',
343+
'input' => 'datetime',
344+
'with_seconds' => true,
345+
'reference_date' => new \DateTimeImmutable('2019-07-12', new \DateTimeZone('UTC')),
346+
'widget' => 'single_text',
347+
]);
348+
$form->setData(new \DateTime('2022-04-29 15:09:10'));
349+
350+
$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
351+
$this->assertSame('17:09:10', $form->getViewData());
352+
}
353+
282354
public function testSubmitDifferentTimezones()
283355
{
284356
$form = $this->factory->create(static::TESTED_TYPE, null, [

0 commit comments

Comments
 (0)
0