Skip to content

Commit ec03721

Browse files
committed
deprecate using date and time types with date objects with not-matching timezones
1 parent bc55b03 commit ec03721

File tree

8 files changed

+145
-3
lines changed

8 files changed

+145
-3
lines changed

UPGRADE-6.3.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ DependencyInjection
77
* Deprecate `PhpDumper` options `inline_factories_parameter` and `inline_class_loader_parameter`, use `inline_factories` and `inline_class_loader` instead
88
* Deprecate undefined and numeric keys with `service_locator` config, use string aliases instead
99

10+
Form
11+
----
12+
13+
* Deprecate using `DateTime` or `DateTimeImmutable` model data with a different timezone than configured with the
14+
`model_timezone` option in `DateType`, `DateTimeType`, and `TimeType`
15+
1016
FrameworkBundle
1117
---------------
1218

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.3
5+
---
6+
7+
* Deprecate using `DateTime` or `DateTimeImmutable` model data with a different timezone than configured with the
8+
`model_timezone` option in `DateType`, `DateTimeType`, and `TimeType`
9+
410
6.2
511
---
612

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
2323
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
2424
use Symfony\Component\Form\FormBuilderInterface;
25+
use Symfony\Component\Form\FormEvent;
26+
use Symfony\Component\Form\FormEvents;
2527
use Symfony\Component\Form\FormInterface;
2628
use Symfony\Component\Form\FormView;
2729
use Symfony\Component\Form\ReversedTransformer;
@@ -195,6 +197,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
195197
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts)
196198
));
197199
}
200+
201+
if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
202+
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
203+
$date = $event->getData();
204+
205+
if (!$date instanceof \DateTimeInterface) {
206+
return;
207+
}
208+
209+
if ($date->getTimezone()->getName() !== $options['model_timezone']) {
210+
trigger_deprecation('symfony/form', '6.3', 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']));
211+
}
212+
});
213+
}
198214
}
199215

200216
public function buildView(FormView $view, FormInterface $form, array $options)

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
2020
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTransformer;
2121
use Symfony\Component\Form\FormBuilderInterface;
22+
use Symfony\Component\Form\FormEvent;
23+
use Symfony\Component\Form\FormEvents;
2224
use Symfony\Component\Form\FormInterface;
2325
use Symfony\Component\Form\FormView;
2426
use Symfony\Component\Form\ReversedTransformer;
@@ -175,6 +177,20 @@ class_exists(\IntlTimeZone::class, false) ? \IntlTimeZone::createDefault() : nul
175177
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], ['year', 'month', 'day'])
176178
));
177179
}
180+
181+
if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
182+
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
183+
$date = $event->getData();
184+
185+
if (!$date instanceof \DateTimeInterface) {
186+
return;
187+
}
188+
189+
if ($date->getTimezone()->getName() !== $options['model_timezone']) {
190+
trigger_deprecation('symfony/form', '6.3', 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']));
191+
}
192+
});
193+
}
178194
}
179195

180196
public function finishView(FormView $view, FormInterface $form, array $options)

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
205205
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts, 'text' === $options['widget'], $options['reference_date'])
206206
));
207207
}
208+
209+
if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
210+
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
211+
$date = $event->getData();
212+
213+
if (!$date instanceof \DateTimeInterface) {
214+
return;
215+
}
216+
217+
if ($date->getTimezone()->getName() !== $options['model_timezone']) {
218+
trigger_deprecation('symfony/form', '6.3', 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']));
219+
}
220+
});
221+
}
208222
}
209223

210224
public function buildView(FormView $view, FormInterface $form, array $options)

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@
1111

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

14+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1415
use Symfony\Component\Form\FormError;
1516
use Symfony\Component\Form\FormInterface;
1617

1718
class DateTimeTypeTest extends BaseTypeTest
1819
{
20+
use ExpectDeprecationTrait;
21+
1922
public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\DateTimeType';
2023

2124
private $defaultLocale;
@@ -154,7 +157,7 @@ public function testSubmitWithoutMinutes()
154157
'with_minutes' => false,
155158
]);
156159

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

159162
$input = [
160163
'date' => [
@@ -184,7 +187,7 @@ public function testSubmitWithSeconds()
184187
'with_seconds' => true,
185188
]);
186189

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

189192
$input = [
190193
'date' => [
@@ -735,4 +738,29 @@ public function testSubmitStringWithCustomInputFormat()
735738

736739
$this->assertSame('14/01/2018 21:29:00 +00:00', $form->getData());
737740
}
741+
742+
/**
743+
* @group legacy
744+
*/
745+
public function testDateTimeInputTimezoneNotMatchingModelTimezone()
746+
{
747+
$this->expectDeprecation('Since symfony/form 6.3: Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
748+
749+
$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
750+
'model_timezone' => 'Europe/Berlin',
751+
]);
752+
}
753+
754+
/**
755+
* @group legacy
756+
*/
757+
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
758+
{
759+
$this->expectDeprecation('Since symfony/form 6.3: Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
760+
761+
$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
762+
'input' => 'datetime_immutable',
763+
'model_timezone' => 'Europe/Berlin',
764+
]);
765+
}
738766
}

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

Lines changed: 29 additions & 1 deletion
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\FormError;
1617
use Symfony\Component\Form\FormInterface;
@@ -19,6 +20,8 @@
1920

2021
class DateTypeTest extends BaseTypeTest
2122
{
23+
use ExpectDeprecationTrait;
24+
2225
public const TESTED_TYPE = 'Symfony\Component\Form\Extension\Core\Type\DateType';
2326

2427
private $defaultTimezone;
@@ -640,7 +643,7 @@ public function testIsSynchronizedReturnsTrueIfChoiceAndCompletelyEmpty()
640643

641644
public function testIsSynchronizedReturnsTrueIfChoiceAndCompletelyFilled()
642645
{
643-
$form = $this->factory->create(static::TESTED_TYPE, new \DateTime(), [
646+
$form = $this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
644647
'model_timezone' => 'UTC',
645648
'view_timezone' => 'UTC',
646649
'widget' => 'choice',
@@ -1083,4 +1086,29 @@ public function testSubmitStringWithCustomInputFormat()
10831086

10841087
$this->assertSame('14/01/2018', $form->getData());
10851088
}
1089+
1090+
/**
1091+
* @group legacy
1092+
*/
1093+
public function testDateTimeInputTimezoneNotMatchingModelTimezone()
1094+
{
1095+
$this->expectDeprecation('Since symfony/form 6.3: Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
1096+
1097+
$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
1098+
'model_timezone' => 'Europe/Berlin',
1099+
]);
1100+
}
1101+
1102+
/**
1103+
* @group legacy
1104+
*/
1105+
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
1106+
{
1107+
$this->expectDeprecation('Since symfony/form 6.3: Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
1108+
1109+
$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
1110+
'input' => 'datetime_immutable',
1111+
'model_timezone' => 'Europe/Berlin',
1112+
]);
1113+
}
10861114
}

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

Lines changed: 28 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()
@@ -1124,4 +1127,29 @@ public function provideEmptyData()
11241127
'Compound choice field lazy' => ['choice', $lazyEmptyData, $expectedData],
11251128
];
11261129
}
1130+
1131+
/**
1132+
* @group legacy
1133+
*/
1134+
public function testDateTimeInputTimezoneNotMatchingModelTimezone()
1135+
{
1136+
$this->expectDeprecation('Since symfony/form 6.3: Using a "DateTime" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
1137+
1138+
$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
1139+
'model_timezone' => 'Europe/Berlin',
1140+
]);
1141+
}
1142+
1143+
/**
1144+
* @group legacy
1145+
*/
1146+
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
1147+
{
1148+
$this->expectDeprecation('Since symfony/form 6.3: Using a "DateTimeImmutable" instance with a timezone ("UTC") not matching the configured model timezone "Europe/Berlin" is deprecated.');
1149+
1150+
$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
1151+
'input' => 'datetime_immutable',
1152+
'model_timezone' => 'Europe/Berlin',
1153+
]);
1154+
}
11271155
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy