Skip to content

Commit 3873d69

Browse files
committed
[Validator] Format datetime values in comparison constraints with "dateFormat" option
1 parent c01b032 commit 3873d69

13 files changed

+37
-6
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
CHANGELOG
22
=========
33

4+
5.3
5+
---
6+
7+
* Add a `dateFormat` option to comparison constraints (`EqualTo`,
8+
`GreaterThanOrEqual`, `GreaterThan`, `LessThanOrEqual`, `LessThan`,
9+
`IndenticalTo`, `NotEqualTo` and `NotIdenticalTo`) to format datetimes in
10+
placeholders:
11+
```php
12+
/**
13+
* @Assert\EqualTo(value="2020-01-01", dateFormat="Y-d-m")
14+
*/
15+
```
16+
will output a message like: `This value should be equal to 2020-01-01.`.
17+
418
5.2.0
519
-----
620

src/Symfony/Component/Validator/ConstraintValidator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,19 @@ protected function formatTypeOf($value)
8484
*
8585
* @return string The string representation of the passed value
8686
*/
87-
protected function formatValue($value, int $format = 0)
87+
protected function formatValue($value, int $format = 0, ?string $dateFormat = null)
8888
{
8989
if (($format & self::PRETTY_DATE) && $value instanceof \DateTimeInterface) {
9090
if (class_exists(\IntlDateFormatter::class)) {
91-
$formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC');
91+
$formatter = new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, 'UTC', null, $dateFormat);
9292

9393
return $formatter->format(new \DateTime(
9494
$value->format('Y-m-d H:i:s.u'),
9595
new \DateTimeZone('UTC')
9696
));
9797
}
9898

99-
return $value->format('Y-m-d H:i:s');
99+
return $value->format($dateFormat ?? 'Y-m-d H:i:s');
100100
}
101101

102102
if (\is_object($value)) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\PropertyAccess\PropertyAccess;
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
17+
use Symfony\Component\Validator\Exception\InvalidOptionsException;
1718
use Symfony\Component\Validator\Exception\LogicException;
1819

1920
/**
@@ -27,6 +28,7 @@ abstract class AbstractComparison extends Constraint
2728
public $message;
2829
public $value;
2930
public $propertyPath;
31+
public $dateFormat;
3032

3133
/**
3234
* {@inheritdoc}
@@ -57,6 +59,12 @@ public function __construct($value = null, $propertyPath = null, string $message
5759
if (null !== $this->propertyPath && !class_exists(PropertyAccess::class)) {
5860
throw new LogicException(sprintf('The "%s" constraint requires the Symfony PropertyAccess component to use the "propertyPath" option.', static::class));
5961
}
62+
63+
$this->dateFormat = $options['dateFormat'] ?? null;
64+
65+
if (null !== $this->dateFormat && !\is_string($this->dateFormat)) {
66+
throw new InvalidOptionsException('The "dateFormat" option must be null or a string.', $options);
67+
}
6068
}
6169

6270
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public function validate($value, Constraint $constraint)
7878

7979
if (!$this->compareValues($value, $comparedValue)) {
8080
$violationBuilder = $this->context->buildViolation($constraint->message)
81-
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
82-
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
81+
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE, $constraint->dateFormat))
82+
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE, $constraint->dateFormat))
8383
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
8484
->setCode($this->getErrorCode());
8585

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ abstract public function provideValidComparisonsToPropertyPath(): array;
179179
* @param mixed $comparedValueString
180180
* @param string $comparedValueType
181181
*/
182-
public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType)
182+
public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType, $dateFormat = null)
183183
{
184184
// Conversion of dates to string differs between ICU versions
185185
// Make sure we have the correct version loaded
@@ -189,6 +189,7 @@ public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $
189189

190190
$constraint = $this->createConstraint(['value' => $comparedValue]);
191191
$constraint->message = 'Constraint Message';
192+
$constraint->dateFormat = $dateFormat;
192193

193194
$this->validator->validate($dirtyValue, $constraint);
194195

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function provideInvalidComparisons(): array
7474
[new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', '2000-01-01', 'Jan 1, 2000, 12:00 AM', 'DateTime'],
7575
[new \DateTime('2001-01-01 UTC'), 'Jan 1, 2001, 12:00 AM', '2000-01-01 UTC', 'Jan 1, 2000, 12:00 AM', 'DateTime'],
7676
[new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
77+
[new \DateTime('2001-01-01'), '2001-01-01', new \DateTime('2000-01-01'), '2000-01-01', 'DateTime', 'Y-MM-dd'],
7778
];
7879
}
7980

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function provideInvalidComparisons(): array
7777
[new \DateTime('2000/01/01'), 'Jan 1, 2000, 12:00 AM', '2005/01/01', 'Jan 1, 2005, 12:00 AM', 'DateTime'],
7878
[new \DateTime('2000/01/01 UTC'), 'Jan 1, 2000, 12:00 AM', '2005/01/01 UTC', 'Jan 1, 2005, 12:00 AM', 'DateTime'],
7979
['b', '"b"', 'c', '"c"', 'string'],
80+
[new \DateTime('2000/01/01'), '2000-01-01', new \DateTime('2005/01/01'), '2005-01-01', 'DateTime', 'Y-MM-dd'],
8081
];
8182
}
8283

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function provideInvalidComparisons(): array
7979
[new ComparisonTest_Class(5), '5', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
8080
['22', '"22"', '333', '"333"', 'string'],
8181
['22', '"22"', '22', '"22"', 'string'],
82+
[new \DateTime('2000/01/01'), '2000-01-01', new \DateTime('2005/01/01'), '2005-01-01', 'DateTime', 'Y-MM-dd'],
8283
];
8384
}
8485

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public function provideInvalidComparisons(): array
9292
[new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', 'DateTime'],
9393
[new \DateTime('2001-01-01'), 'Jan 1, 2001, 12:00 AM', new \DateTime('1999-01-01'), 'Jan 1, 1999, 12:00 AM', 'DateTime'],
9494
[new ComparisonTest_Class(4), '4', new ComparisonTest_Class(5), '5', __NAMESPACE__.'\ComparisonTest_Class'],
95+
[new \DateTime('2001-01-01'), '2001-01-01', new \DateTime('2001-01-01'), '2001-01-01', 'DateTime', 'Y-MM-dd'],
9596
];
9697
}
9798

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function provideInvalidComparisons(): array
8080
[new \DateTime('2010-01-01 UTC'), 'Jan 1, 2010, 12:00 AM', '2000-01-01 UTC', 'Jan 1, 2000, 12:00 AM', 'DateTime'],
8181
[new ComparisonTest_Class(5), '5', new ComparisonTest_Class(4), '4', __NAMESPACE__.'\ComparisonTest_Class'],
8282
['c', '"c"', 'b', '"b"', 'string'],
83+
[new \DateTime('2010-01-01'), '2010-01-01', new \DateTime('2000-01-01'), '2000-01-01', 'DateTime', 'Y-MM-dd'],
8384
];
8485
}
8586

0 commit comments

Comments
 (0)