8000 [Validator] Deprecate validating DateTimeInterface in Date|Time|DateTime constraints by ro0NL · Pull Request #25015 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Deprecate validating DateTimeInterface in Date|Time|DateTime constraints #25015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE-4.2.md
10000
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,4 @@ Validator

* The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead
* The `ValidatorBuilderInterface` has been deprecated and `ValidatorBuilder` made final
* 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.
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ Validator
* Removed the `checkDNS` and `dnsMessage` options from the `Url` constraint.
* The component is now decoupled from `symfony/translation` and uses `Symfony\Contracts\Translation\TranslatorInterface` instead
* The `ValidatorBuilderInterface` has been removed and `ValidatorBuilder` is now final
* 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.

Workflow
--------
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ CHANGELOG
* decoupled from `symfony/translation` by using `Symfony\Contracts\Translation\TranslatorInterface`
* deprecated `ValidatorBuilderInterface`
* made `ValidatorBuilder` final
* marked `format` the default option in `DateTime` constraint
* deprecated validating instances of `\DateTimeInterface` in `DateTimeValidator`, `DateValidator` and `TimeValidator`.

4.1.0
-----
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ class DateTime extends Constraint

public $format = 'Y-m-d H:i:s';
public $message = 'This value is not a valid datetime.';

public function getDefaultOption()
{
return 'format';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateTime');
}

if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
if (null === $value || '' === $value) {
return;
}

if ($value instanceof \DateTimeInterface) {
@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);

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Date');
}

if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
if (null === $value || '' === $value) {
return;
}

if ($value instanceof \DateTimeInterface) {
@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);

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Time');
}

if (null === $value || '' === $value || $value instanceof \DateTimeInterface) {
if (null === $value || '' === $value) {
return;
}

if ($value instanceof \DateTimeInterface) {
@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);

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ public function testEmptyStringIsValid()
$this->assertNoViolation();
}

/**
* @group legacy
* @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.
*/
public function testDateTimeClassIsValid()
{
$this->validator->validate(new \DateTime(), new DateTime());

$this->assertNoViolation();
}

/**
* @group legacy
* @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.
*/
public function testDateTimeImmutableClassIsValid()
{
$this->validator->validate(new \DateTimeImmutable(), new DateTime());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,21 @@ public function testEmptyStringIsValid()
$this->assertN F92B oViolation();
}

/**
* @group legacy
* @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.
*/
public function testDateTimeClassIsValid()
{
$this->validator->validate(new \DateTime(), new Date());

$this->assertNoViolation();
}

/**
* @group legacy
* @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.
*/
public function testDateTimeImmutableClassIsValid()
{
$this->validator->validate(new \DateTimeImmutable(), new Date());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function testEmptyStringIsValid()
$this->assertNoViolation();
}

/**
* @group legacy
* @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.
*/
public function testDateTimeClassIsValid()
{
$this->validator->validate(new \DateTime(), new Time());
Expand Down Expand Up @@ -100,6 +104,10 @@ public function getInvalidTimes()
);
}

/**
* @group legacy
* @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.
*/
public function testDateTimeImmutableIsValid()
{
$this->validator->validate(new \DateTimeImmutable(), new Time());
Expand Down
0