derive the view timezone from the model timezone

This commit is contained in:
Christian Flothmann 2020-01-04 09:15:56 +01:00
parent aa9ccf8825
commit ef5835d833
3 changed files with 26 additions and 4 deletions

View File

@ -4,6 +4,7 @@ CHANGELOG
5.1.0
-----
* The `view_timezone` option defaults to the `model_timezone` if no `reference_date` is configured.
* Added default `inputmode` attribute to Search, Email and Tel form types.
5.0.0

View File

@ -280,6 +280,18 @@ class TimeType extends AbstractType
return null;
};
$viewTimezone = static function (Options $options, $value): ?string {
if (null !== $value) {
return $value;
}
if (null !== $options['model_timezone'] && null === $options['reference_date']) {
return $options['model_timezone'];
}
return null;
};
$resolver->setDefaults([
'hours' => range(0, 23),
'minutes' => range(0, 59),
@ -290,7 +302,7 @@ class TimeType extends AbstractType
'with_minutes' => true,
'with_seconds' => false,
'model_timezone' => $modelTimezone,
'view_timezone' => null,
'view_timezone' => $viewTimezone,
'reference_date' => null,
'placeholder' => $placeholderDefault,
'html5' => true,
@ -310,12 +322,12 @@ class TimeType extends AbstractType
'choice_translation_domain' => false,
]);
$resolver->setNormalizer('model_timezone', function (Options $options, $modelTimezone): ?string {
if (null !== $modelTimezone && $options['view_timezone'] !== $modelTimezone && null === $options['reference_date']) {
$resolver->setNormalizer('view_timezone', function (Options $options, $viewTimezone): ?string {
if (null !== $options['model_timezone'] && $viewTimezone !== $options['model_timezone'] && null === $options['reference_date']) {
throw new LogicException(sprintf('Using different values for the "model_timezone" and "view_timezone" options without configuring a reference date is not supported.'));
}
return $modelTimezone;
return $viewTimezone;
});
$resolver->setNormalizer('placeholder', $placeholderNormalizer);

View File

@ -859,6 +859,15 @@ class TimeTypeTest extends BaseTypeTest
$this->assertSame('Europe/Berlin', $form->getConfig()->getOption('model_timezone'));
}
public function testViewTimezoneDefaultsToModelTimezoneIfProvided()
{
$form = $this->factory->create(static::TESTED_TYPE, null, [
'model_timezone' => 'Europe/Berlin',
]);
$this->assertSame('Europe/Berlin', $form->getConfig()->getOption('view_timezone'));
}
public function testPassDefaultChoiceTranslationDomain()
{
$form = $this->factory->create(static::TESTED_TYPE);