Skip to content

Commit

Permalink
feature #35205 [Form] derive the view timezone from the model timezon…
Browse files Browse the repository at this point in the history
…e (xabbuh)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[Form] derive the view timezone from the model timezone

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | Fix #32805
| License       | MIT
| Doc PR        |

Commits
-------

ef5835d derive the view timezone from the model timezone
  • Loading branch information
fabpot committed Jan 7, 2020
2 parents a1c3f55 + ef5835d commit c29b2a1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Expand Up @@ -280,6 +280,18 @@ public function configureOptions(OptionsResolver $resolver)
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),
Expand All @@ -290,7 +302,7 @@ public function configureOptions(OptionsResolver $resolver)
'with_minutes' => true,
'with_seconds' => false,
'model_timezone' => $modelTimezone,
'view_timezone' => null,
'view_timezone' => $viewTimezone,
'reference_date' => null,
'placeholder' => $placeholderDefault,
'html5' => true,
Expand All @@ -310,12 +322,12 @@ public function configureOptions(OptionsResolver $resolver)
'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);
Expand Down
Expand Up @@ -859,6 +859,15 @@ public function testModelTimezoneDefaultToReferenceDateTimezoneIfProvided()
$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);
Expand Down

0 comments on commit c29b2a1

Please sign in to comment.