@@ -289,6 +289,40 @@ However, if the ``personal_email`` field does not exist in the array,
289
289
the ``NotBlank `` constraint will still be applied (since it is wrapped in
290
290
``Required ``) and you will receive a constraint violation.
291
291
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
+
292
326
Options
293
327
-------
294
328
0 commit comments