8000 [Validator] [DateTime] Add `format` to error messages · symfony/symfony@20e83b9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 20e83b9

Browse files
sauliusnordfabpot
authored andcommitted
[Validator] [DateTime] Add format to error messages
1 parent b4f121a commit 20e83b9

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

src/Symfony/Bridge/Monolog/Tests/Handler/ChromePhpHandlerTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ class ChromePhpHandlerTest extends TestCase
2222
{
2323
public function testOnKernelResponseShouldNotTriggerDeprecation()
2424
{
25-
$this->expectNotToPerformAssertions();
26-
2725
$request = Request::create('/');
2826
$request->headers->remove('User-Agent');
2927

3028
$response = new Response('foo');
3129
$event = new ResponseEvent($this->createMock(HttpKernelInterface::class), $request, HttpKernelInterface::MAIN_REQUEST, $response);
3230

31+
$error = null;
32+
set_error_handler(function ($type, $message) use (&$error) { $error = $message; }, \E_DEPRECATED);
33+
3334
$listener = new ChromePhpHandler();
3435
$listener->onKernelResponse($event);
36+
restore_error_handler();
37+
38+
$this->assertNull($error);
3539
}
3640
}

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CHANGELOG
1818
* Add the `Week` constraint
1919
* Add `CompoundConstraintTestCase` to ease testing Compound Constraints
2020
* Add context variable to `WhenValidator`
21+
* Add `format` parameter to `DateTime` constraint violation message
2122

2223
7.1
2324
---

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public function validate(mixed $value, Constraint $constraint): void
4444
if (0 < $errors['error_count']) {
4545
$this->context->buildViolation($constraint->message)
4646
->setParameter('{{ value }}', $this->formatValue($value))
47+
->setParameter('{{ format }}', $this->formatValue($constraint->format))
4748
->setCode(DateTime::INVALID_FORMAT_ERROR)
4849
->addViolation();
4950

@@ -58,16 +59,19 @@ public function validate(mixed $value, Constraint $constraint): void
5859
if ('The parsed date was invalid' === $warning) {
5960
$this->context->buildViolation($constraint->message)
6061
->setParameter('{{ value }}', $this->formatValue($value))
62+
->setParameter('{{ format }}', $this->formatValue($constraint->format))
6163
->setCode(DateTime::INVALID_DATE_ERROR)
6264
->addViolation();
6365
} elseif ('The parsed time was invalid' === $warning) {
6466
$this->context->buildViolation($constraint->message)
6567
->setParameter('{{ value }}', $this->formatValue($value))
68+
->setParameter('{{ format }}', $this->formatValue($constraint->format))
6669
->setCode(DateTime::INVALID_TIME_ERROR)
6770
->addViolation();
6871
} else {
6972
$this->context->buildViolation($constraint->message)
7073
->setParameter('{{ value }}', $this->formatValue($value))
74+
->setParameter('{{ format }}', $this->formatValue($constraint->format))
7175
->setCode(DateTime::INVALID_FORMAT_ERROR)
7276
->addViolation();
7377
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function testDateTimeWithDefaultFormat()
5353

5454
$this->buildViolation('This value is not a valid datetime.')
5555
->setParameter('{{ value }}', '"1995-03-24"')
56+
->setParameter('{{ format }}', '"Y-m-d H:i:s"')
5657
->setCode(DateTime::INVALID_FORMAT_ERROR)
5758
->assertRaised();
5859
}
@@ -96,6 +97,7 @@ public function testInvalidDateTimes($format, $dateTime, $code)
9697

9798
$this->buildViolation('myMessage')
9899
->setParameter('{{ value }}', '"'.$dateTime.'"')
100+
->setParameter('{{ format }}', '"'.$format.'"')
99101
->setCode($code)
100102
->assertRaised();
101103
}
@@ -124,6 +126,7 @@ public function testInvalidDateTimeNamed()
124126

125127
$this->buildViolation('myMessage')
126128
->setParameter('{{ value }}', '"2010-01-01 00:00:00"')
129+
->setParameter('{{ format }}', '"Y-m-d"')
127130
->setCode(DateTime::INVALID_FORMAT_ERROR)
128131
->assertRaised();
129132
}

0 commit comments

Comments
 (0)
0