8000 [Form] fix populating single widget time view data with different timezones by xabbuh · Pull Request #46216 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] fix populating single widget time view data with different timezones #46216

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

Merged
merged 1 commit into from
Apr 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
* @param string|null $inputTimezone The name of the input timezone
* @param string|null $outputTimezone The name of the output timezone
* @param string $format The date format
* @param string|null $parseFormat The parse format when different from $format
*/
public function __construct(string $inputTimezone = null, string $outputTimezone = null, string $format = 'Y-m-d H:i:s')
public function __construct(string $inputTimezone = null, string $outputTimezone = null, string $format = 'Y-m-d H:i:s', string $parseFormat = null)
{
parent::__construct($inputTimezone, $outputTimezone);

$this->generateFormat = $this->parseFormat = $format;
$this->generateFormat = $format;
$this->parseFormat = $parseFormat ?? $format;

// See https://php.net/datetime.createfromformat
// The character "|" in the format makes sure that the parts of a date
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}
});

$parseFormat = null;

if (null !== $options['reference_date']) {
$format = 'Y-m-d '.$format;
$parseFormat = 'Y-m-d '.$format;

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
$data = $event->getData();
Expand All @@ -85,7 +87,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
});
}

$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format, $parseFormat));
} else {
$hourOptions = $minuteOptions = $secondOptions = [
'error_bubbling' => true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,76 @@ public function testSubmitWithSecondsAndBrowserOmissionSeconds()
$this->assertEquals('03:04:00', $form->getViewData());
}

public function testPreSetDataDifferentTimezones()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'Europe/Berlin',
'input' => 'datetime',
'with_seconds' => true,
'reference_date' => new \DateTimeImmutable('2019-01-01', new \DateTimeZone('UTC')),
]);
$form->setData(new \DateTime('2022-01-01 15:09:10', new \DateTimeZone('UTC')));

$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
$this->assertSame([
'hour' => '16',
'minute' => '9',
'second' => '10',
], $form->getViewData());
}

public function testPreSetDataDifferentTimezonesDuringDaylightSavingTime()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'Europe/Berlin',
'input' => 'datetime',
'with_seconds' => true,
'reference_date' => new \DateTimeImmutable('2019-07-12', new \DateTimeZone('UTC')),
]);
$form->setData(new \DateTime('2022-04-29 15:09:10', new \DateTimeZone('UTC')));

$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
$this->assertSame([
'hour' => '17',
'minute' => '9',
'second' => '10',
], $form->getViewData());
}

public function testPreSetDataDifferentTimezonesUsingSingleTextWidget()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'Europe/Berlin',
'input' => 'datetime',
'with_seconds' => true,
'reference_date' => new \DateTimeImmutable('2019-01-01', new \DateTimeZone('UTC')),
'widget' => 'single_text',
]);
$form->setData(new \DateTime('2022-01-01 15:09:10', new \DateTimeZone('UTC')));

$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
$this->assertSame('16:09:10', $form->getViewData());
}

public function testPreSetDataDifferentTimezonesDuringDaylightSavingTimeUsingSingleTextWidget()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'UTC',
'view_timezone' => 'Europe/Berlin',
'input' => 'datetime',
'with_seconds' => true,
'reference_date' => new \DateTimeImmutable('2019-07-12', new \DateTimeZone('UTC')),
'widget' => 'single_text',
]);
$form->setData(new \DateTime('2022-04-29 15:09:10', new \DateTimeZone('UTC')));

$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
$this->assertSame('17:09:10', $form->getViewData());
}

public function testSubmitDifferentTimezones()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
Expand Down
0