@@ -9,6 +9,7 @@ Applies to :ref:`class <validation-class-target>`
9
9
or :ref: `property/method <validation-property-target >`
10
10
Options - `expression `_
11
11
- `constraints `_
12
+ _ `otherwise `_
12
13
- `groups `_
13
14
- `payload `_
14
15
- `values `_
@@ -47,7 +48,7 @@ properties::
47
48
To validate the object, you have some requirements:
48
49
49
50
A) If ``type `` is ``percent ``, then ``value `` must be less than or equal 100;
50
- B) If ``type `` is `` absolute ``, then ``value `` can be anything ;
51
+ B) If ``type `` is not `` percent ``, then ``value `` must be less than 9999 ;
51
52
C) No matter the value of ``type ``, the ``value `` must be greater than 0.
52
53
53
54
One way to accomplish this is with the When constraint:
@@ -69,6 +70,9 @@ One way to accomplish this is with the When constraint:
69
70
constraints: [
70
71
new Assert\LessThanOrEqual(100, message: 'The value should be between 1 and 100!')
71
72
],
73
+ otherwise: [
74
+ new Assert\LessThan(9999, message: 'The value should be less than 9999!')
75
+ ],
72
76
)]
73
77
private ?int $value;
74
78
@@ -88,6 +92,10 @@ One way to accomplish this is with the When constraint:
88
92
- LessThanOrEqual :
89
93
value : 100
90
94
message : " The value should be between 1 and 100!"
95
+ otherwise :
96
+ - LessThan :
97
+ value : 9999
98
+ message : " The value should be less than 9999!"
91
99
92
100
.. code-block :: xml
93
101
@@ -109,6 +117,12 @@ One way to accomplish this is with the When constraint:
109
117
<option name =" message" >The value should be between 1 and 100!</option >
110
118
</constraint >
111
119
</option >
120
+ <option name =" otherwise" >
121
+ <constraint name =" LessThan" >
122
+ <option name =" value" >9999</option >
123
+ <option name =" message" >The value should be less than 9999!</option >
124
+ </constraint >
125
+ </option >
112
126
</constraint >
113
127
</property >
114
128
</class >
@@ -135,6 +149,12 @@ One way to accomplish this is with the When constraint:
135
149
message: 'The value should be between 1 and 100!',
136
150
),
137
151
],
152
+ otherwise: [
153
+ new Assert\LessThan(
154
+ value: 9999,
155
+ message: 'The value should be less than 9999!',
156
+ ),
157
+ ],
138
158
));
139
159
}
140
160
@@ -279,6 +299,17 @@ You can also pass custom variables using the `values`_ option.
279
299
280
300
One or multiple constraints that are applied if the expression returns true.
281
301
302
+ ``otherwise ``
303
+ ~~~~~~~~~~~~~
304
+
305
+ **type **: ``array|Constraint ``
306
+
307
+ One or multiple constraints that are applied if the expression returns false.
308
+
309
+ .. versionadded :: 7.3
310
+
311
+ The ``otherwise `` option was introduced in Symfony 7.3.
312
+
282
313
.. include :: /reference/constraints/_groups-option.rst.inc
283
314
284
315
.. include :: /reference/constraints/_payload-option.rst.inc
0 commit comments