8000 [Form] deprecate using the date and time types with date objects with not-matching timezones by xabbuh · Pull Request #46426 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] deprecate using the date and time types with date objects with not-matching timezones #46426

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 8000 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
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
6 changes: 6 additions & 0 deletions UPGRADE-6.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ DoctrineBridge
* Deprecate not constructing `DoctrineDataCollector` with an instance of `DebugDataHolder`
* Deprecate `DoctrineDataCollector::addLogger()`, use a `DebugDataHolder` instead

Form
----

* Deprecate using `DateTime` or `DateTimeImmutable` model data with a different timezone than configured with the
`model_timezone` option in `DateType`, `DateTimeType`, and `TimeType`

HttpFoundation
--------------

Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

6.4
---

* Deprecate using `DateTime` or `DateTimeImmutable` model data with a different timezone than configured with the
`model_timezone` option in `DateType`, `DateTimeType`, and `TimeType`

6.3
---

Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/DateTimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\ReversedTransformer;
Expand Down Expand Up @@ -194,6 +196,21 @@ public function buildForm(FormBuilderInterface $builder, array $options)
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts)
));
}

if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
$date = $event->getData();

if (!$date instanceof \DateTimeInterface) {
return;
}

if ($date->getTimezone()->getName() !== $options['model_timezone']) {
trigger_deprecation('symfony/form', '6.4', sprintf('Using a "%s" instance with a timezone ("%s") not matching the configured model timezone "%s" is deprecated.', $date::class, $date->getTimezone()->getName(), $options['model_timezone']));
// throw new LogicException(sprintf('Using a "%s" instance with a timezone ("%s") not matching the configured model timezone "%s" is not supported.', $date::class, $date->getTimezone()->getName(), $options['model_timezone']));
}
});
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\ReversedTransformer;
Expand Down Expand Up @@ -178,6 +180,21 @@ class_exists(\IntlTimeZone::class, false) ? \IntlTimeZone::createDefault() : nul
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], ['year', 'month', 'day'])
));
}

if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
$date = $event->getData();

if (!$date instanceof \DateTimeInterface) {
return;
}

if ($date->getTimezone()->getName() !== $options['model_timezone']) {
trigger_deprecation('symfony/form', '6.4', sprintf('Using a "%s" instance with a timezone ("%s") not matching the configured model timezone "%s" is deprecated.', $date::class, $date->getTimezone()->getName(), $options['model_timezone']));
// throw new LogicException(sprintf('Using a "%s" instance with a timezone ("%s") not matching the configured model timezone "%s" is not supported.', $date::class, $date->getTimezone()->getName(), $options['model_timezone']));
}
});
}
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,21 @@ public function buildForm(FormBuilderInterface $builder, array $options)
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts, 'text' === $options['widget'], $options['reference_date'])
));
}

if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
$date = $event->getData();

if (!$date instanceof \DateTimeInterface) {
return;
}

if ($date->getTimezone()->getName() !== $options['model_timezone']) {
trigger_deprecation('symfony/form', '6.4', sprintf('Using a "%s" instance with a timezone ("%s") not matching the configured model timezone "%s" is deprecated.', $date::class, $date->getTimezone()->getName(), $options['model_timezone']));
// throw new LogicException(sprintf('Using a "%s" instance with a timezone ("%s") not matching the configured model timezone "%s" is not supported.', $date::class, $date->getTimezone()->getName(), $options['model_timezone']));
}
});
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;

class DateTimeTypeTest extends BaseTypeTestCase
{
use ExpectDeprecationTrait;

public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\DateTimeType';

private $defaultLocale;
Expand Down Expand Up @@ -154,7 +157,7 @@ public function testSubmitWithoutMinutes()
'with_minutes' => false,
]);

$form->setData(new \DateTime());
$form->setData(new \DateTime('now', new \DateTimeZone('UTC')));

$input = [
'date' => [
Expand Down Expand Up @@ -184,7 +187,7 @@ public function testSubmitWithSeconds()
'with_seconds' => true,
]);

$form->setData(new \DateTime());
$form->setData(new \DateTime('now', new \DateTimeZone('UTC')));

$input = [
'date' => [
Expand Down Expand Up @@ -748,6 +751,35 @@ public function testSubmitStringWithCustomInputFormat()
$this->assertSame('14/01/2018 21:29:00 +00:00', $form->getData());
}

/**
* @group legacy
*/
A3E2 public function testDateTimeInputTimezoneNotMatchingModelTimezone()
{
$this->expectDeprecation('Since symfony/form 6.4: Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
// $this->expectException(LogicException::class);
// $this->expectExceptionMessage('Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is not supported.');

$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
'model_timezone' => 'Europe/Berlin',
]);
}

/**
* @group legacy
*/
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
{
$this->expectDeprecation('Since symfony/form 6.4: Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
// $this->expectException(LogicException::class);
// $this->expectExceptionMessage('Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is not supported.');

$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
'input' => 'datetime_immutable',
'model_timezone' => 'Europe/Berlin',
]);
}

protected function getTestOptions(): array
{
return ['widget' => 'choice'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
Expand All @@ -19,6 +20,8 @@

class DateTypeTest extends BaseTypeTestCase
{
use ExpectDeprecationTrait;

public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\DateType';

private $defaultTimezone;
Expand Down Expand Up @@ -654,7 +657,7 @@ public function testIsSynchronizedReturnsTrueIfChoiceAndCompletelyEmpty()

public function testIsSynchronizedReturnsTrueIfChoiceAndCompletelyFilled()
{
$form = $this->factory->create(static::TESTED_TYPE, new \DateTime(), [
$form = $this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'choice',
Expand Down Expand Up @@ -1112,6 +1115,35 @@ public function testSubmitStringWithCustomInputFormat()
$this->assertSame('14/01/2018', $form->getData());
}

/**
* @group legacy
*/
public function testDateTimeInputTimezoneNotMatchingModelTimezone()
{
$this->expectDeprecation('Since symfony/form 6.4: Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
// $this->expectException(LogicException::class);
// $this->expectExceptionMessage('Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is not supported.');

$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
'model_timezone' => 'Europe/Berlin',
]);
}

/**
* @group legacy
*/
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
{
$this->expectDeprecation('Since symfony/form 6.4: Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
// $this->expectException(LogicException::class);
// $this->expectExceptionMessage('Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is not supported.');

$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
'input' => 'datetime_immutable',
'model_timezone' => 'Europe/Berlin',
]);
}

protected function getTestOptions(): array
{
return ['widget' => 'choice'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Symfony\Component\Form\Exception\InvalidConfigurationException;
use Symfony\Component\Form\Exception\LogicException;
Expand All @@ -20,6 +21,8 @@

class TimeTypeTest extends BaseTypeTestCase
{
use ExpectDeprecationTrait;

public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\TimeType';

public function testSubmitDateTime()
Expand Down Expand Up @@ -1161,6 +1164,35 @@ public static function provideEmptyData()
];
}

/**
* @group legacy
*/
public function testDateTimeInputTimezoneNotMatchingModelTimezone()
{
$this->expectDeprecation('Since symfony/form 6.4: Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
// $this->expectException(LogicException::class);
// $this->expectExceptionMessage('Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is not supported.');

$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
'model_timezone' => 'Europe/Berlin',
]);
}

/**
* @group legacy
*/
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
{
$this->expectDeprecation('Since symfony/form 6.4: Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
// $this->expectException(LogicException::class);
// $this->expectExceptionMessage('Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is not supported.');

$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
'input' => 'datetime_immutable',
'model_timezone' => 'Europe/Berlin',
]);
}

protected function getTestOptions(): array
{
return ['widget' => 'choice'];
Expand Down
0