8000 feature #58559 [Validator] [DateTime] Add `format` to error messages … · symfony/symfony@fc65c8f · GitHub
[go: up one dir, main page]

Skip to content

Commit fc65c8f

Browse files
committed
feature #58559 [Validator] [DateTime] Add format to error messages (sauliusnord)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Validator] [DateTime] Add `format` to error messages | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT ## Problem Currently there is no way to dynamically add the `format` value into error messages when using the `DateTime` constraint. It has to be hardcoded on every `DateTime` constraint. For example: ```php #[Assert\DateTime(format: 'Y-m-d', message: 'Value {{ value }} is invalid. Expected format: "Y-m-d"')] public string $from; #[Assert\DateTime(format: 'Y-m-d', message: 'Value {{ value }} is invalid. Expected format: "Y-m-d"')] public string $to; ``` ## Goal The goal is to add the possibility to use a parameter instead of hardcoding the selected format. For example: ```php #[Assert\DateTime(format: 'Y-m-d', message: 'Value {{ value }} is invalid. Expected format:{{ format }}')] public string $from; ``` If using the [symfony translator](https://github.com/symfony/translation) we would even not need to set the message when creating the constraint. For example: ```php $translator = new Translator('en'); $translator->addLoader('array', new ArrayLoader()); $translator->addResource('array', [ 'This value is not a valid datetime.' => 'Value {{ value }} is invalid. Expected format:{{ format }}', ], 'en'); ``` ## Solution When building the violations set a new parameter called `format` which represents the given `format` when the `DateTime` constraint is created. Commits ------- 20e83b9 [Validator] [DateTime] Add `format` to error messages
2 parents e33dbfa + 20e83b9 commit fc65c8f

File tree

4 files changed

+14
-2
lines changed

4 files changed

+14
-2
lines changed

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

+6-2
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

+1
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

+4
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

+3
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(< 57AE span class=pl-s1>$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