Skip to content

Commit 197da33

Browse files
[Validator] Add support for the otherwise option in the When constraint
1 parent 0b180d5 commit 197da33

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

reference/constraints/When.rst

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Applies to :ref:`class <validation-class-target>`
99
or :ref:`property/method <validation-property-target>`
1010
Options - `expression`_
1111
- `constraints`_
12+
_ `otherwise`_
1213
- `groups`_
1314
- `payload`_
1415
- `values`_
@@ -47,7 +48,7 @@ properties::
4748
To validate the object, you have some requirements:
4849

4950
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;
5152
C) No matter the value of ``type``, the ``value`` must be greater than 0.
5253

5354
One way to accomplish this is with the When constraint:
@@ -69,6 +70,9 @@ One way to accomplish this is with the When constraint:
6970
constraints: [
7071
new Assert\LessThanOrEqual(100, message: 'The value should be between 1 and 100!')
7172
],
73+
otherwise: [
74+
new Assert\LessThan(9999, message: 'The value should be less than 9999!')
75+
],
7276
)]
7377
private ?int $value;
7478
@@ -88,6 +92,10 @@ One way to accomplish this is with the When constraint:
8892
- LessThanOrEqual:
8993
value: 100
9094
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!"
9199
92100
.. code-block:: xml
93101
@@ -109,6 +117,12 @@ One way to accomplish this is with the When constraint:
109117
<option name="message">The value should be between 1 and 100!</option>
110118
</constraint>
111119
</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>
112126
</constraint>
113127
</property>
114128
</class>
@@ -135,6 +149,12 @@ One way to accomplish this is with the When constraint:
135149
message: 'The value should be between 1 and 100!',
136150
),
137151
],
152+
otherwise: [
153+
new Assert\LessThan(
154+
value: 9999,
155+
message: 'The value should be less than 9999!',
156+
),
157+
],
138158
));
139159
}
140160
@@ -279,6 +299,17 @@ You can also pass custom variables using the `values`_ option.
279299

280300
One or multiple constraints that are applied if the expression returns true.
281301

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+
282313
.. include:: /reference/constraints/_groups-option.rst.inc
283314

284315
.. include:: /reference/constraints/_payload-option.rst.inc

0 commit comments

Comments
 (0)