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

Skip to content

Commit ef5835d

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

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

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
5.1.0
55
-----
66

7+
* The `view_timezone` option defaults to the `model_timezone` if no `reference_date` is configured.
78
* Added default `inputmode` attribute to Search, Email and Tel form types.
89

910
5.0.0

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,18 @@ 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'] && null === $options['reference_date']) {
289+
return $options['model_timezone'];
290+
}
291+
292+
return null;
293+
};
294+
283295
$resolver->setDefaults([
284296
'hours' => range(0, 23),
285297
'minutes' => range(0, 59),
@@ -290,7 +302,7 @@ public function configureOptions(OptionsResolver $resolver)
290302
'with_minutes' => true,
291303
'with_seconds' => false,
292304
'model_timezone' => $modelTimezone,
293-
'view_timezone' => null,
305+
'view_timezone' => $viewTimezone,
294306
'reference_date' => null,
295307
'placeholder' => $placeholderDefault,
296308
'html5' => true,
@@ -310,12 +322,12 @@ public function configureOptions(OptionsResolver $resolver)
310322
'choice_translation_domain' => false,
311323
]);
312324

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

318-
return $modelTimezone;
330+
return $viewTimezone;
319331
});
320332

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

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,15 @@ 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+
862871
public function testPassDefaultChoiceTranslationDomain()
863872
{
864873
$form = $this->factory->create(static::TESTED_TYPE);

0 commit comments

Comments
 (0)
0