Commit fc65c8f
committed
feature symfony#58559 [Validator] [DateTime] Add
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 messagesformat to error messages (sauliusnord)File tree
4 files changed
+14
-2
lines changed- src/Symfony
- Bridge/Monolog/Tests/Handler
- Component/Validator
- Constraints
- Tests/Constraints
4 files changed
+14
-2
lines changedLines changed: 6 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | 25 | | |
28 | 26 | | |
29 | 27 | | |
30 | 28 | | |
31 | 29 | | |
32 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
35 | 39 | | |
36 | 40 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
| |||
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
| 62 | + | |
61 | 63 | | |
62 | 64 | | |
63 | 65 | | |
64 | 66 | | |
65 | 67 | | |
| 68 | + | |
66 | 69 | | |
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
70 | 73 | | |
| 74 | + | |
71 | 75 | | |
72 | 76 | | |
73 | 77 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
| |||
96 | 97 | | |
97 | 98 | | |
98 | 99 | | |
| 100 | + | |
99 | 101 | | |
100 | 102 | | |
101 | 103 | | |
| |||
124 | 126 | | |
125 | 127 | | |
126 | 128 | | |
| 129 | + | |
127 | 130 | | |
128 | 131 | | |
129 | 132 | | |
| |||
0 commit comments