Skip to content

Commit 5c3962e

Browse files
bug symfony#18759 [Validator] Support for DateTimeImmutable (krzysiekpiasecki)
This PR was squashed before being merged into the 3.0 branch (closes symfony#18759). Discussion ---------- [Validator] Support for DateTimeImmutable | Q | A | ------------- | --- | Branch? | 3.0 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#18752 | License | MIT | Doc PR | When validating with DateTime constraint UnexpectedTypeException is thrown for DateTimeImmutable instances. Why PR? - DateTimeImmutable behaves like a DateTime. Both implements the same interface DateTimeInterface. - DateTimeInterface cannot be implemented by the client. Commits ------- f49659f [Validator] Support for DateTimeImmutable
2 parents 720dac1 + f49659f commit 5c3962e

File tree

4 files changed

+16
-2
lines changed

4 files changed

+16
-2
lines changed

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

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

33-
if (null === $value || '' === $value || $value instanceof \DateTime) {
33+
if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
3434
return;
3535
}
3636

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

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

50-
if (null === $value || '' === $value || $value instanceof \DateTime) {
50+
if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
5151
return;
5252
}
5353

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public function testDateTimeClassIsValid()
4242
$this->assertNoViolation();
4343
}
4444

45+
public function testDateTimeImmutableClassIsValid()
46+
{
47+
$this->validator->validate(new \DateTimeImmutable(), new DateTime());
48+
49+
$this->assertNoViolation();
50+
}
51+
4552
/**
4653
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
4754
*/

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public function testDateTimeClassIsValid()
4242
$this->assertNoViolation();
4343
}
4444

45+
public function testDateTimeImmutableClassIsValid()
46+
{
47+
$this->validator->validate(new \DateTimeImmutable(), new Date());
48+
49+
$this->assertNoViolation();
50+
}
51+
4552
/**
4653
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
4754
*/

0 commit comments

Comments
 (0)