10000 [Validator] Deprecate validating DateTimeInterface in Date|Time|DateT… · symfony/symfony@5454e6f · GitHub
[go: up one dir, main page]

Skip to content

Commit 5454e6f

Browse files
committed
[Validator] Deprecate validating DateTimeInterface in Date|Time|DateTime constraints
1 parent f2f4cd8 commit 5454e6f

File tree

10 files changed

+54
-3
lines changed

10 files changed

+54
-3
lines changed

UPGRADE-4.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,4 @@ Validator
162162

163163
* The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead
164164
* The `ValidatorBuilderInterface` has been deprecated and `ValidatorBuilder` made final
165+
* Deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`. Use `Type` instead or remove the constraint if the underlying model is type hinted to `\DateTimeInterface` already.

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ Validator
163163
* Removed the `checkDNS` and `dnsMessage` options from the `Url` constraint.
164164
* The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead
165165
* The `ValidatorBuilderInterface` has been removed and `ValidatorBuilder` is now final
166+
* Removed support for validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`. Use `Type` instead or remove the constraint if the underlying model is type hinted to `\DateTimeInterface` already.
166167

167168
Workflow
168169
--------

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ CHANGELOG
88
* decoupled from `symfony/translation` by using `Symfony\Contracts\Translation\TranslatorInterface`
99
* deprecated `ValidatorBuilderInterface`
1010
* made `ValidatorBuilder` final
11+
* marked `format` the default option in `DateTime` constraint
12+
* deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`.
1113

1214
4.1.0
1315
-----

src/Symfony/Component/Validator/Constraints/DateTime.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ class DateTime extends Co 10000 nstraint
3333

3434
public $format = 'Y-m-d H:i:s';
3535
public $message = 'This value is not a valid datetime.';
36+
37+
public function getDefaultOption()
38+
{
39+
return 'format';
40+
}
3641
}

src/Symfony/Component/Validator/Constraints/DateTimeValidator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ public function validate($value, Constraint $constraint)
2929
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateTime');
3030
}
3131

32-
if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
32+
if (null === $value || '' === $value) {
33+
return;
34+
}
35+
36+
if ($value instanceof \DateTimeInterface) {
37+
@trigger_error(sprintf('Validating a \\DateTimeInterface with "%s" is deprecated since version 4.2. Use "%s" instead or remove the constraint if the underlying model is already type hinted to \\DateTimeInterface.', DateTime::class, Type::class), E_USER_DEPRECATED);
38+
3339
return;
3440
}
3541

src/Symfony/Component/Validator/Constraints/DateValidator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ public function validate($value, Constraint $constraint)
4747
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Date');
4848
}
4949

50-
if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
50+
if (null === $value || '' === $value) {
51+
return;
52+
}
53+
54+
if ($value instanceof \DateTimeInterface) {
55+
@trigger_error(sprintf('Validating a \\DateTimeInterface with "%s" is deprecated since version 4.2. Use "%s" instead or remove the constraint if the underlying model is already type hinted to \\DateTimeInterface.', Date::class, Type::class), E_USER_DEPRECATED);
56+
5157
return;
5258
}
5359

src/Symfony/Component/Validator/Constraints/TimeValidator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ public function validate($value, Constraint $constraint)
4747
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Time');
4848
}
4949

50-
if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
50+
if (null === $value || '' === $value) {
51+
return;
52+
}
53+
54+
if ($value instanceof \DateTimeInterface) {
55+
@trigger_error(sprintf('Validating a \\DateTimeInterface with "%s" is deprecated since version 4.2. Use "%s" instead or remove the constraint if the underlying model is already type hinted to \\DateTimeInterface.', Time::class, Type::class), E_USER_DEPRECATED);
56+
5157
return;
5258
}
5359

src/Symfony/Component/Validator/Tests/Constraints/DateTimeValidatorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,21 @@ public function testEmptyStringIsValid()
3636
$this->assertNoViolation();
3737
}
3838

39+
/**
40+
* @group legacy
41+
* @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\DateTime" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface.
42+
*/
3943
public function testDateTimeClassIsValid()
4044
{
4145
$this->validator->validate(new \DateTime(), new DateTime());
4246

4347
$this->assertNoViolation();
4448
}
4549

50+
/**
51+
* @group legacy
52+
* @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\DateTime" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface.
53+
*/
4654
public function testDateTimeImmutableClassIsValid()
4755
{
4856
$this->validator->validate(new \DateTimeImmutable(), new DateTime());

src/Symfony/Component/Validator/Tests/Constraints/DateValidatorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,21 @@ public function testEmptyStringIsValid()
3636
$this->assertNoViolation();
3737
}
3838

39+
/**
40+
* @group legacy
41+
* @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\Date" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface.
42+
*/
3943
public function testDateTimeClassIsValid()
4044
{
4145
$this->validator->validate(new \DateTime(), new Date());
4246

4347
$this->assertNoViolation();
4448
}
4549

50+
/**
51+
* @group legacy
52+
* @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\Date" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface.
53+
*/
4654
public function testDateTimeImmutableClassIsValid()
4755
{
4856
$this->validator->validate(new \DateTimeImmutable(), new Date());

src/Symfony/Component/Validator/Tests/Constraints/TimeValidatorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public function testEmptyStringIsValid()
3636
$this->assertNoViolation();
3737
}
3838

39+
/**
40+
* @group legacy
41+
* @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\Time" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface.
42+
*/
3943
public function testDateTimeClassIsValid()
4044
7E05 {
4145
$this->validator->validate(new \DateTime(), new Time());
@@ -100,6 +104,10 @@ public function getInvalidTimes()
100104
);
101105
}
102106

107+
/**
108+
* @group legacy
109+
* @expectedDeprecation Validating a \DateTimeInterface with "Symfony\Component\Validator\Constraints\Time" is deprecated since version 4.2. Use "Symfony\Component\Validator\Constraints\Type" instead or remove the constraint if the underlying model is already type hinted to \DateTimeInterface.
110+
*/
103111
public function testDateTimeImmutableIsValid()
104112
{
105113
$this->validator->validate(new \DateTimeImmutable(), new Time());

0 commit comments

Comments
 (0)
0