Skip to content

Commit 30aea31

Browse files
committed
Merge branch '4.4' into 5.2
* 4.4: [Validator] Added grouped constraints in `Collection` reference [#15074] fix typo
2 parents 337a76d + b357089 commit 30aea31

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
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

security/voters.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ There are three strategies available:
279279

280280
``consensus``
281281
This grants access if there are more voters granting access than
282-
denying. If case of a tie the decision is based on the
282+
denying. In case of a tie the decision is based on the
283283
``allow_if_equal_granted_denied`` config option (defaulting to ``true``);
284284

285285
``unanimous``

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)