8000 deprecate using date and time types with date objects with not-matchi… · symfony/symfony@4a73df7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a73df7

Browse files
committed
deprecate using date and time types with date objects with not-matching timezones
1 parent c782174 commit 4a73df7

File tree

9 files changed

+149
-3
lines changed

9 files changed

+149
-3
lines changed

UPGRADE-6.2.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ DependencyInjection
2323
Form
2424
----
2525

26+
* Deprecate using `DateTime` or `DateTimeImmutable` model data with a different timezone than configured with the
27+
`model_timezone` option in `DateType`, `DateTimeType`, and `TimeType`
2628
* Deprecate calling `Button/Form::setParent()`, `ButtonBuilder/FormConfigBuilder::setDataMapper()`, `TransformationFailedException::setInvalidMessage()` without arguments
2729
* Change the signature of `FormConfigBuilderInterface::setDataMapper()` to `setDataMapper(?DataMapperInterface)`
2830
* Change the signature of `FormInterface::setParent()` to `setParent(?self)`

UPGRADE-6.3.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
UPGRADE FROM 6.2 to 6.3
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 in `DateType`, `DateTimeType`, and `TimeType`

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;
@@ -197,6 +199,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
197199
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts)
198200
));
199201
}
202+
203+
if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
204+
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
205+
$date = $event->getData();
206+
207+
if (!$date instanceof \DateTimeInterface) {
208+
return;
209+
}
210+
211+
if ($date->getTimezone()->getName() !== $options['model_timezone']) {
212+
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']));
213+
}
214+
});
215+
}
200216
}
201217

202218
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;
@@ -177,6 +179,20 @@ class_exists(\IntlTimeZone::class, false) ? \IntlTimeZone::createDefault() : nul
177179
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], ['year', 'month', 'day'])
178180
));
179181
}
182+
183+
if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
184+
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
185+
$date = $event->getData();
186+
187+
if (!$date instanceof \DateTimeInterface) {
188+
return;
189+
}
190+
191+
if ($date->getTimezone()->getName() !== $options['model_timezone']) {
192+
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']));
193+
}
194+
});
195+
}
180196
}
181197

182198
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
@@ -207,6 +207,20 @@ public function buildForm(FormBuilderInterface $builder, array $options)
207207
new DateTimeToArrayTransformer($options['model_timezone'], $options['model_timezone'], $parts, 'text' === $options['widget'], $options['reference_date'])
208208
));
209209
}
210+
211+
if (\in_array($options['input'], ['datetime', 'datetime_immutable'], true) && null !== $options['model_timezone']) {
212+
$builder->addEventListener(FormEvents::POST_SET_DATA, static function (FormEvent $event) use ($options): void {
213+
$date = $event->getData();
214+
215+
if (!$date instanceof \DateTimeInterface) {
216+
return;
217+
}
218+
219+
if ($date->getTimezone()->getName() !== $options['model_timezone']) {
220+
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']));
221+
}
222+
});
223+
}
210224
}
211225

212226
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' => [
@@ -737,4 +740,29 @@ public function testSubmitStringWithCustomInputFormat()
737740

738741
$this->assertSame('14/01/2018 21:29:00 +00:00', $form->getData());
739742
}
743+
744+
/**
745+
* @group legacy
746+
*/
747+
public function testDateTimeInputTimezoneNotMatchingModelTimezone()
748+
{
749+
$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.');
750+
751+
$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
752+
'model_timezone' => 'Europe/Berlin',
753+
]);
754+
}
755+
756+
/**
757+
* @group legacy
758+
*/
759+
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
760+
{
761+
$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.');
762+
763+
$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
764+
'input' => 'datetime_immutable',
765+
'model_timezone' => 'Europe/Berlin',
766+
]);
767+
}
740768
}

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

Lines changed: 29 additions & 1 deletion
< 10000 tr class="diff-line-row">
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',
@@ -1085,4 +1088,29 @@ public function testSubmitStringWithCustomInputFormat()
10851088

10861089
$this->assertSame('14/01/2018', $form->getData());
10871090
}
1091+
1092+
/**
1093+
* @group legacy
1094+
*/
1095+
public function testDateTimeInputTimezoneNotMatchingModelTimezone()
1096+
{
1097+
$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.');
1098+
1099+
$this->factory->create(static::TESTED_TYPE, new \DateTime('now', new \DateTimeZone('UTC')), [
1100+
'model_timezone' => 'Europe/Berlin',
1101+
]);
1102+
}
1103+
1104+
/**
1105+
* @group legacy
1106+
*/
1107+
public function testDateTimeImmutableInputTimezoneNotMatchingModelTimezone()
1108+
{
1109+
$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.');
1110+
1111+
$this->factory->create(static::TESTED_TYPE, new \DateTimeImmutable('now', new \DateTimeZone('UTC')), [
1112+
'input' => 'datetime_immutable',
1113+
'model_timezone' => 'Europe/Berlin',
1114+
]);
1115+
}
10881116
}

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()
@@ -1126,4 +1129,29 @@ public function provideEmptyData()
11261129
'Compound 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+
'input' => 'datetime_immutable',
1154+
'model_timezone' => 'Europe/Berlin',
1155+
]);
1156+
}
11291157
}

0 commit comments

Comments
 (0)
0