diff --git a/reference/constraints/When.rst b/reference/constraints/When.rst index ec21eca9784..22ae477365f 100644 --- a/reference/constraints/When.rst +++ b/reference/constraints/When.rst @@ -9,6 +9,7 @@ Applies to :ref:`class ` or :ref:`property/method ` Options - `expression`_ - `constraints`_ + _ `otherwise`_ - `groups`_ - `payload`_ - `values`_ @@ -47,7 +48,7 @@ properties:: To validate the object, you have some requirements: A) If ``type`` is ``percent``, then ``value`` must be less than or equal 100; -B) If ``type`` is ``absolute``, then ``value`` can be anything; +B) If ``type`` is not ``percent``, then ``value`` must be less than 9999; C) No matter the value of ``type``, the ``value`` must be greater than 0. One way to accomplish this is with the When constraint: @@ -69,6 +70,9 @@ One way to accomplish this is with the When constraint: constraints: [ new Assert\LessThanOrEqual(100, message: 'The value should be between 1 and 100!') ], + otherwise: [ + new Assert\LessThan(9999, message: 'The value should be less than 9999!') + ], )] private ?int $value; @@ -88,6 +92,10 @@ One way to accomplish this is with the When constraint: - LessThanOrEqual: value: 100 message: "The value should be between 1 and 100!" + otherwise: + - LessThan: + value: 9999 + message: "The value should be less than 9999!" .. code-block:: xml @@ -109,6 +117,12 @@ One way to accomplish this is with the When constraint: + @@ -135,6 +149,12 @@ One way to accomplish this is with the When constraint: message: 'The value should be between 1 and 100!', ), ], + otherwise: [ + new Assert\LessThan( + value: 9999, + message: 'The value should be less than 9999!', + ), + ], )); } @@ -279,6 +299,17 @@ You can also pass custom variables using the `values`_ option. One or multiple constraints that are applied if the expression returns true. +``otherwise`` +~~~~~~~~~~~~~ + +**type**: ``array|Constraint`` + +One or multiple constraints that are applied if the expression returns false. + +.. versionadded:: 7.3 + + The ``otherwise`` option was introduced in Symfony 7.3. + .. include:: /reference/constraints/_groups-option.rst.inc .. include:: /reference/constraints/_payload-option.rst.inc