Skip to content

Commit cf8f937

Browse files
HeahDudeOskarStark
authored andcommitted
[Validator] Added grouped constraints in Collection reference
1 parent c4dd584 commit cf8f937

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

reference/constraints/Collection.rst

Lines 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

Lines 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy