Skip to content

Commit c218ef2

Browse files
committed
minor #21433 [Validator] deprecate passing choices as $options argument to Choice constraint (javiereguiluz)
This PR was merged into the 7.4 branch. Discussion ---------- [Validator] deprecate passing choices as $options argument to Choice constraint Fixes #21258. Commits ------- 17080a6 [Validator] deprecate passing choices as $options argument to Choice constraint
2 parents 4465a03 + 17080a6 commit c218ef2

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

reference/constraints/Choice.rst

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ If your valid choice list is simple, you can pass them in directly via the
3434
{
3535
public const GENRES = ['fiction', 'non-fiction'];
3636
37-
#[Assert\Choice(['New York', 'Berlin', 'Tokyo'])]
37+
#[Assert\Choice(choices: ['New York', 'Berlin', 'Tokyo'])]
3838
protected string $city;
3939
4040
#[Assert\Choice(choices: Author::GENRES, message: 'Choose a valid genre.')]
@@ -47,7 +47,8 @@ If your valid choice list is simple, you can pass them in directly via the
4747
App\Entity\Author:
4848
properties:
4949
city:
50-
- Choice: [New York, Berlin, Tokyo]
50+
- Choice:
51+
choices: [New York, Berlin, Tokyo]
5152
genre:
5253
- Choice:
5354
choices: [fiction, non-fiction]
@@ -64,9 +65,11 @@ If your valid choice list is simple, you can pass them in directly via the
6465
<class name="App\Entity\Author">
6566
<property name="city">
6667
<constraint name="Choice">
67-
<value>New York</value>
68-
<value>Berlin</value>
69-
<value>Tokyo</value>
68+
<option name="choices">
69+
<value>New York</value>
70+
<value>Berlin</value>
71+
<value>Tokyo</value>
72+
</option>
7073
</constraint>
7174
</property>
7275
<property name="genre">
@@ -97,7 +100,7 @@ If your valid choice list is simple, you can pass them in directly via the
97100
{
98101
$metadata->addPropertyConstraint(
99102
'city',
100-
new Assert\Choice(['New York', 'Berlin', 'Tokyo'])
103+
new Assert\Choice(choices: ['New York', 'Berlin', 'Tokyo'])
101104
);
102105
103106
$metadata->addPropertyConstraint('genre', new Assert\Choice(
@@ -107,6 +110,12 @@ If your valid choice list is simple, you can pass them in directly via the
107110
}
108111
}
109112
113+
.. deprecated:: 7.4
114+
115+
Passing an array of choices as the first argument of the ``Choice`` constraint
116+
is deprecated and will stop working in Symfony 8.0. Instead, pass the choices
117+
using the ``choices:`` named argument.
118+
110119
Supplying the Choices with a Callback Function
111120
----------------------------------------------
112121

validation/raw_values.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Validation of arrays is possible using the ``Collection`` constraint::
7575
]),
7676
'email' => new Assert\Email(),
7777
'simple' => new Assert\Length(['min' => 102]),
78-
'eye_color' => new Assert\Choice([3, 4]),
78+
'eye_color' => new Assert\Choice(choices: [3, 4]),
7979
'file' => new Assert\File(),
8080
'password' => new Assert\Length(['min' => 60]),
8181
'tags' => new Assert\Optional([

0 commit comments

Comments
 (0)