8000 deprecate using the TimeType with date objects with not-matching time… · symfony/symfony@9baadc8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9baadc8

Browse files
committed
deprecate using the TimeType with date objects with not-matching timezones
1 parent 16befcd commit 9baadc8

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

UPGRADE-6.2.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
UPGRADE FROM 6.1 to 6.2
2+
=======================
3+
4+
Form
5+
----
6+
7+
* Deprecate using `DateTime` or `DateTimeImmutable` model data with a different timezone than configured with the
8+
`model_timezone` option.

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
6.2
5+
---
6+
7+
* Deprecate using `DateTime` or `DateTimeImmutable` model data with a different timezone than configured with the
8+
`model_timezone` option.
9+
410
6.1
511
---
612

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
210210
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts, 'text' === $options['widget'], $options['reference_date'])
211211
));
212212
}
213+
214+
if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
215+
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
216+
$date = $event->getData();
217+
218+
if (!$date instanceof \DateTimeInterface) {
219+
return;
220+
}
221+
222+
if ($date->getTimezone()->getName() !== $options['model_timezone']) {
223+
trigger_deprecation('symfony/form', '6.2', sprintf('Using a "%s" instance with a timezone ("%s") not matching the configured model timezone "%s" is deprecated.', \get_class($date), $date->getTimezone()->getName(), $options['model_timezone']));
224+
}
225+
});
226+
}
213227
}
214228

215229
/**

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

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

14+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1415
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
1516
use Symfony\Component\Form\Exception\InvalidConfigurationException;
1617
use Symfony\Component\Form\Exception\LogicException;
@@ -20,6 +21,8 @@
2021

2122
class TimeTypeTest extends BaseTypeTest
2223
{
24+
use ExpectDeprecationTrait;
25+
2326
public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\TimeType';
2427

2528
public function testSubmitDateTime()
@@ -1126,4 +1129,28 @@ public function provideEmptyData()
11261129
'Compound 8000 choice field lazy' => ['choice', $lazyEmptyData, $expectedData],
11271130
];
11281131
}
1132+
1133+
/**
1134+
* @group legacy
1135+
*/
1136+
public function testDateTimeInputTimezoneNotMatchingModelTimezone()
1137+
{
1138+
$this->expectDeprecation('Since symfony/form 6.2: Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
1139+
1140+
$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
1141+
'model_timezone' => 'Europe/Berlin',
1142+
]);
1143+
}
1144+
1145+
/**
1146+
* @group legacy
1147+
*/
1148+
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
1149+
{
1150+
$this->expectDeprecation('Since symfony/form 6.2: Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
1151+
1152+
$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
1153+
'model_timezone' => 'Europe/Berlin',
1154+
]);
1155+
}
11291156
}

0 commit comments

Comments
 (0)
0