@@ -526,6 +526,7 @@ to an array callback::
526
526
527
527
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
528
528
529
+ // ...
529
530
public function setDefaultOptions(OptionsResolverInterface $resolver)
530
531
{
531
532
$resolver->setDefaults(array(
@@ -541,23 +542,51 @@ This will call the static method ``determineValidationGroups()`` on the
541
542
The Form object is passed as an argument to that method (see next example).
542
543
You can also define whole logic inline by using a ``Closure ``::
543
544
545
+ use Acme\AcmeBundle\Entity\Client;
544
546
use Symfony\Component\Form\FormInterface;
545
547
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
546
548
549
+ // ...
547
550
public function setDefaultOptions(OptionsResolverInterface $resolver)
548
551
{
549
552
$resolver->setDefaults(array(
550
553
'validation_groups' => function(FormInterface $form) {
551
554
$data = $form->getData();
552
- if (Entity\ Client::TYPE_PERSON == $data->getType()) {
555
+ if (Client::TYPE_PERSON == $data->getType()) {
553
556
return array('person');
554
- } else {
555
- return array('company');
556
557
}
558
+
559
+ return array('company');
560
+ },
561
+ ));
562
+ }
563
+
564
+ Using the ``validation_groups `` option overrides the default validation
565
+ group which is being used. If you want to validate the default constraints
566
+ of the entity as well you have to adjust the option as follows::
567
+
568
+ use Acme\AcmeBundle\Entity\Client;
569
+ use Symfony\Component\Form\FormInterface;
570
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
571
+
572
+ // ...
573
+ public function setDefaultOptions(OptionsResolverInterface $resolver)
574
+ {
575
+ $resolver->setDefaults(array(
576
+ 'validation_groups' => function(FormInterface $form) {
577
+ $data = $form->getData();
578
+ if (Client::TYPE_PERSON == $data->getType()) {
579
+ return array('Default', 'person');
580
+ }
581
+
582
+ return array('Default', 'company');
557
583
},
558
584
));
559
585
}
560
586
587
+ You can find more information about how the validation groups and the default constraints
588
+ work in the book section about :ref: `validation groups <book-validation-validation-groups >`.
589
+
561
590
.. index ::
562
591
single: Forms; Validation groups based on clicked button
563
592
0 commit comments