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

Appearance settings

Commit cf8f937

Browse filesBrowse files
HeahDudeOskarStark
authored andcommitted
[Validator] Added grouped constraints in Collection reference
1 parent c4dd584 commit cf8f937
Copy full SHA for cf8f937

File tree

Expand file treeCollapse file tree

2 files changed

+41
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+41
-0
lines changed

‎reference/constraints/Collection.rst

Copy file name to clipboardExpand all lines: reference/constraints/Collection.rst
+34Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,40 @@ However, if the ``personal_email`` field does not exist in the array,
289289
the ``NotBlank`` constraint will still be applied (since it is wrapped in
290290
``Required``) and you will receive a constraint violation.
291291

292+
When you define groups in nested constraints they are automatically added to
293+
the ``Collection`` constraint itself so it can be traversed for all nested
294+
groups. Take the following example::
295+
296+
use Symfony\Component\Validator\Constraints as Assert;
297+
298+
$constraint = new Assert\Collection([
299+
'fields' => [
300+
'name' => new Assert\NotBlank(['groups' => 'basic']),
301+
'email' => new Assert\NotBlank(['groups' => 'contact']),
302+
],
303+
]);
304+
305+
This will result in the following configuration::
306+
307+
$constraint = new Assert\Collection([
308+
'fields' => [
309+
'name' => new Assert\Required([
310+
'constraints' => new Assert\NotBlank(['groups' => 'basic']),
311+
'groups' => ['basic', 'strict'],
312+
]),
313+
'email' => new Assert\Required([
314+
"constraints" => new Assert\NotBlank(['groups' => 'contact']),
315+
'groups' => ['basic', 'strict'],
316+
]),
317+
],
318+
'groups' => ['basic', 'strict'],
319+
]);
320+
321+
The default ``allowMissingFields`` option requires the fields in all groups.
322+
So when validating in ``contact`` group, ``$name`` can be empty but the key is
323+
still required. If this is not the intended behavior, use the ``Optional``
324+
constraint explicitly instead of ``Required``.
325+
292326
Options
293327
-------
294328

‎validation/raw_values.rst

Copy file name to clipboardExpand all lines: validation/raw_values.rst
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,10 @@ The ``validate()`` method returns a :class:`Symfony\\Component\\Validator\\Const
105105
object, which acts like an array of errors. Each error in the collection
106106
is a :class:`Symfony\\Component\\Validator\\ConstraintViolation` object,
107107
which holds the error message on its ``getMessage()`` method.
108+
109+
.. note::
110+
111+
When using groups with the
112+
:doc:`Collection</reference/constraints/Collection>` constraint, be sure to
113+
use the ``Optional`` constraint when appropriate as explained in its
114+
reference documentation.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.