Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] Update DateTimeValidator to validate with format only #21905

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions UPGRADE-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ TwigBundle

* The `ContainerAwareRuntimeLoader` class has been deprecated and will be removed in 4.0.
Use the Twig `Twig_ContainerRuntimeLoader` class instead.

Validator
---------

* `DateValidator` and `TimeValidator` have been deprecated. Use `DateTimeValidator` instead.

Workflow
--------
Expand Down
2 changes: 2 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ Validator
changed to `true` as of 4.0. If you need the previous behaviour ensure to
set the option to `false`.

* `DateValidator` and `TimeValidator` have been removed. Use `DateTimeValidator` instead.

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 @@ -6,6 +6,8 @@ CHANGELOG

* added `AddValidatorInitializersPass`
* added `AddConstraintValidatorsPass`
* deprecated `DateValidator` in favor of `DateTimeValidator`
* deprecated `TimeValidator` in favor of `DateTimeValidator`

3.2.0
-----
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

namespace Symfony\Component\Validator\Constraints;

@trigger_error('The '.__NAMESPACE__.'\Date class is deprecated since version 3.3 and will be removed in 4.0. Use '.__NAMESPACE__.'\DateTime instead.', E_USER_DEPRECATED);

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Bernhard Schussek <[email protected]>
*
* @deprecated Since version 3.3, to be removed in 4.0. Use Symfony\Component\Validator\Constraints\DateTimeValidator instead.
*/
class Date extends Constraint
{
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@
class DateTime extends Constraint
{
const INVALID_FORMAT_ERROR = '1a9da513-2640-4f84-9b6a-4d99dcddc628';

/**
* @deprecated since version 3.3, to be removed in 4.0.
*/
const INVALID_DATE_ERROR = 'd52afa47-620d-4d99-9f08-f4d85b36e33c';

/**
* @deprecated since version 3.3, to be removed in 4.0.
*/
const INVALID_TIME_ERROR = '5e797c9d-74f7-4098-baa3-94390c447b27';

protected static $errorNames = array(
Expand All @@ -33,4 +41,12 @@ class DateTime extends Constraint

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

/**
* {@inheritdoc}
*/
public function getDefaultOption()
{
return 'format';
}
}
24 changes: 3 additions & 21 deletions src/Symfony/Component/Validator/Constraints/DateTimeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <[email protected]>
* @author Diego Saint Esteben <[email protected]>
*/
class DateTimeValidator extends DateValidator
class DateTimeValidator extends ConstraintValidator
{
/**
* @deprecated since version 3.1, to be removed in 4.0.
Expand Down Expand Up @@ -48,32 +49,13 @@ public function validate($value, Constraint $constraint)

$errors = \DateTime::getLastErrors();

if (0 < $errors['error_count']) {
if (0 < $errors['error_count'] || 0 < $errors['warning_count']) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(DateTime::INVALID_FORMAT_ERROR)
->addViolation();

return;
}

foreach ($errors['warnings'] as $warning) {
if ('The parsed date was invalid' === $warning) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(DateTime::INVALID_DATE_ERROR)
->addViolation();
} elseif ('The parsed time was invalid' === $warning) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(DateTime::INVALID_TIME_ERROR)
->addViolation();
} else {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(DateTime::INVALID_FORMAT_ERROR)
->addViolation();
}
}
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

namespace Symfony\Component\Validator\Constraints;

@trigger_error('The '.__NAMESPACE__.'\DateValidator class is deprecated since version 3.3 and will be removed in 4.0. Use '.__NAMESPACE__.'\DateTimeValidator instead.', E_USER_DEPRECATED);

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <[email protected]>
*
* @deprecated Since version 3.3, to be removed in 4.0. Use Symfony\Component\Validator\Constraints\DateTimeValidator instead.
*/
class DateValidator extends ConstraintValidator
{
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

namespace Symfony\Component\Validator\Constraints;

@trigger_error('The '.__NAMESPACE__.'\Time class is deprecated since version 3.3 and will be removed in 4.0. Use '.__NAMESPACE__.'\DateTime instead.', E_USER_DEPRECATED);

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
* @Target({"PROPERTY", "METHOD", "ANNOTATION"})
*
* @author Bernhard Schussek <[email protected]>
*
* @deprecated Since version 3.3, to be removed in 4.0. Use Symfony\Component\Validator\Constraints\DateTimeValidator instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you also need to add the E_USER_DEPRECATED on top of the file

*/
class Time extends Constraint
{
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/TimeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

namespace Symfony\Component\Validator\Constraints;

@trigger_error('The '.__NAMESPACE__.'\TimeValidator class is deprecated since version 3.3 and will be removed in 4.0. Use '.__NAMESPACE__.'\DateTimeValidator instead.', E_USER_DEPRECATED);

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <[email protected]>
*
* @deprecated Since version 3.3, to be removed in 4.0. Use Symfony\Component\Validator\Constraints\DateTimeValidator instead.
*/
class TimeValidator extends ConstraintValidator
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ protected function createValidator()

public function testNullIsValid()
{
$this->validator->validate(null, new DateTime());
$this->validator->validate(null, new DateTime('Y-m-d H:i:s'));

$this->assertNoViolation();
}

public function testEmptyStringIsValid()
{
$this->validator->validate('', new DateTime());
$this->validator->validate('', new DateTime('Y-m-d H:i:s'));

$this->assertNoViolation();
}

public function testDateTimeClassIsValid()
{
$this->validator->validate(new \DateTime(), new DateTime());
$this->validator->validate(new \DateTime(), new DateTime('Y-m-d H:i:s'));

$this->assertNoViolation();
}

public function testDateTimeImmutableClassIsValid()
{
$this->validator->validate(new \DateTimeImmutable(), new DateTime());
$this->validator->validate(new \DateTimeImmutable(), new DateTime('Y-m-d H:i:s'));

$this->assertNoViolation();
}
Expand All @@ -55,16 +55,16 @@ public function testDateTimeImmutableClassIsValid()
*/
public function testExpectsStringCompatibleType()
{
$this->validator->validate(new \stdClass(), new DateTime());
$this->validator->validate(new \stdClass(), new DateTime('Y-m-d H:i:s'));
}

public function testDateTimeWithDefaultFormat()
{
$this->validator->validate('1995-05-10 19:33:00', new DateTime());
$this->validator->validate('1995-05-10 19:33:00', new DateTime('Y-m-d H:i:s'));

$this->assertNoViolation();

$this->validator->validate('1995-03-24', new DateTime());
$this->validator->validate('1995-03-24', new DateTime('Y-m-d H:i:s'));

$this->buildViolation('This value is not a valid datetime.')
->setParameter('{{ value }}', '"1995-03-24"')
Expand Down Expand Up @@ -122,12 +122,12 @@ public function getInvalidDateTimes()
array('H:i', '00:00:00', DateTime::INVALID_FORMAT_ERROR),
array('Y-m-d', '2010-01-01 00:00', DateTime::INVALID_FORMAT_ERROR),
array('Y-m-d e', '2010-01-01 TCU', DateTime::INVALID_FORMAT_ERROR),
array('Y-m-d H:i:s', '2010-13-01 00:00:00', DateTime::INVALID_DATE_ERROR),
array('Y-m-d H:i:s', '2010-04-32 00:00:00', DateTime::INVALID_DATE_ERROR),
array('Y-m-d H:i:s', '2010-02-29 00:00:00', DateTime::INVALID_DATE_ERROR),
array('Y-m-d H:i:s', '2010-01-01 24:00:00', DateTime::INVALID_TIME_ERROR),
array('Y-m-d H:i:s', '2010-01-01 00:60:00', DateTime::INVALID_TIME_ERROR),
array('Y-m-d H:i:s', '2010-01-01 00:00:60', DateTime::INVALID_TIME_ERROR),
array('Y-m-d H:i:s', '2010-13-01 00:00:00', DateTime::INVALID_FORMAT_ERROR),
array('Y-m-d H:i:s', '2010-04-32 00:00:00', DateTime::INVALID_FORMAT_ERROR),
array('Y-m-d H:i:s', '2010-02-29 00:00:00', DateTime::INVALID_FORMAT_ERROR),
array('Y-m-d H:i:s', '2010-01-01 24:00:00', DateTime::INVALID_FORMAT_ERROR),
array('Y-m-d H:i:s', '2010-01-01 00:60:00', DateTime::INVALID_FORMAT_ERROR),
array('Y-m-d H:i:s', '2010-01-01 00:00:60', DateTime::INVALID_FORMAT_ERROR),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Symfony\Component\Validator\Constraints\DateValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

/**
* @group legacy
*/
class DateValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use Symfony\Component\Validator\Constraints\TimeValidator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

/**
* @group legacy
*/
class TimeValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator()
Expand Down