@@ -94,7 +94,7 @@ following:
94
94
profileData :
95
95
- Collection :
96
96
fields :
97
- personal_email :
97
+ personal_email :
98
98
- Email : ~
99
99
short_bio :
100
100
- NotBlank : ~
@@ -287,6 +287,40 @@ However, if the ``personal_email`` field does not exist in the array,
287
287
the ``NotBlank `` constraint will still be applied (since it is wrapped in
288
288
``Required ``) and you will receive a constraint violation.
289
289
290
+ When you define groups in nested constraints they are automatically added to
291
+ the ``Collection `` constraint itself so it can be traversed for all nested
292
+ groups. Take the following example::
293
+
294
+ use Symfony\Component\Validator\Constraints as Assert;
295
+
296
+ $constraint = new Assert\Collection([
297
+ 'fields' => [
298
+ 'name' => new Assert\NotBlank(['groups' => 'basic']),
299
+ 'email' => new Assert\NotBlank(['groups' => 'contact']),
300
+ ],
301
+ ]);
302
+
303
+ This will result in the following configuration::
304
+
305
+ $constraint = new Assert\Collection([
306
+ 'fields' => [
307
+ 'name' => new Assert\Required([
308
+ 'constraints' => new Assert\NotBlank(['groups' => 'basic']),
309
+ 'groups' => ['basic', 'strict'],
310
+ ]),
311
+ 'email' => new Assert\Required([
312
+ "constraints" => new Assert\NotBlank(['groups' => 'contact']),
313
+ 'groups' => ['basic', 'strict'],
314
+ ]),
315
+ ],
316
+ 'groups' => ['basic', 'strict'],
317
+ ]);
318
+
319
+ The default ``allowMissingFields `` option requires the fields in all groups.
320
+ So when validating in ``contact `` group, ``$name `` can be empty but the key is
321
+ still required. If this is not the intended behavior, use the ``Optional ``
322
+ constraint explicitly instead of ``Required ``.
323
+
290
324
Options
291
325
-------
292
326
0 commit comments