8000 derive the view timezone from the model timezone · symfony/symfony@b1b2f42 · GitHub
[go: up one dir, main page]

Skip to content

Commit b1b2f42

Browse files
committed
derive the view timezone from the model timezone
1 parent aa9ccf8 commit b1b2f42

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,22 @@ public function configureOptions(OptionsResolver $resolver)
280280
return null;
281281
};
282282

283+
$viewTimezone = static function (Options $options, $value): ?string {
284+
if (null !== $value) {
285+
return $value;
286+
}
287+
288+
if (null !== $options['model_timezone']) {
289+
return $options['model_timezone'];
290+
}
291+
292+
if (null !== $options['reference_date']) {
293+
return $options['reference_date']->getTimezone()->getName();
294+
}
295+
296+
return null;
297+
};
298+
283299
$resolver->setDefaults([
284300
'hours' => range(0, 23),
285301
'minutes' => range(0, 59),
@@ -290,7 +306,7 @@ public function configureOptions(OptionsResolver $resolver)
290306
'with_minutes' => true,
291307
'with_seconds' => false,
292308
'model_timezone' => $modelTimezone,
293-
'view_timezone' => null,
309+
'view_timezone' => $viewTimezone,
294310
'reference_date' => null,
295311
'placeholder' => $placeholderDefault,
296312
'html5' => true,
@@ -310,12 +326,12 @@ public function configureOptions(OptionsResolver $resolver)
310326
'choice_translation_domain' => false,
311327
]);
312328

313-
$resolver->setNormalizer('model_timezone', function (Options $options, $modelTimezone): ?string {
314-
if (null !== $modelTimezone && $options['view_timezone'] !== $modelTimezone && null === $options['reference_date']) {
329+
$resolver->setNormalizer('view_timezone', function (Options $options, $viewTimezone): ?string {
330+
if (null !== $viewTimezone && $options['model_timezone'] !== $viewTimezone && null === $options['reference_date']) {
315331
throw new LogicException(sprintf('Using different values for the "model_timezone" and "view_timezone" options without configuring a reference date is not supported.'));
316332
}
317333

318-
return $modelTimezone;
334+
return $viewTimezone;
319335
});
320336

321337
$resolver->setNormalizer('placeholder', $placeholderNormalizer);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,25 @@ public function testModelTimezoneDefaultToReferenceDateTimezoneIfProvided()
859859
$this->assertSame('Europe/Berlin', $form->getConfig()->getOption('model_timezone'));
860860
}
861861

862+
public function testViewTimezoneDefaultsToModelTimezoneIfProvided()
863+
{
864+
$form = $this->factory->create(static::TESTED_TYPE, null, [
865+
'model_timezone' => 'Europe/Berlin',
866+
]);
867+
868+
$this->assertSame('Europe/Berlin', $form->getConfig()->getOption('view_timezone'));
869+
}
870+
871+
public function testModelAndViewTimezoneDefaultToReferenceDateTimezoneIfProvided()
872+
{
873+
$form = $this->factory->create(static::TESTED_TYPE, null, [
874+
'reference_date' => new \DateTimeImmutable('now', new \DateTimeZone('Europe/Berlin')),
875+
]);
876+
877+
$this->assertSame('Europe/Berlin', $form->getConfig()->getOption('model_timezone'));
878+
$this->assertSame('Europe/Berlin', $form->getConfig()->getOption('view_timezone'));
879+
}
880+
862881
public function testPassDefaultChoiceTranslationDomain()
863882
{
864883
$form = $this->factory->create(static::TESTED_TYPE);

0 commit comments

Comments
 (0)
0