Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Validator] Add support for theotherwise option in theWhen constraint#20627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletionreference/constraints/When.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@ Applies to :ref:`class <validation-class-target>`
or :ref:`property/method <validation-property-target>`
Options - `expression`_
- `constraints`_
_ `otherwise`_
- `groups`_
- `payload`_
- `values`_
Expand DownExpand Up@@ -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 beanything;
B) If ``type`` isnot ``percent``, then ``value``must beless 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:
Expand All@@ -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;

Expand All@@ -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

Expand All@@ -109,6 +117,12 @@ One way to accomplish this is with the When constraint:
<option name="message">The value should be between 1 and 100!</option>
</constraint>
</option>
<option name="otherwise">
<constraint name="LessThan">
<option name="value">9999</option>
<option name="message">The value should be less than 9999!</option>
</constraint>
</option>
</constraint>
</property>
</class>
Expand All@@ -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!',
),
],
));
}

Expand DownExpand Up@@ -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
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp