Skip to content

Commit 81728b6

Browse files
peterrehmweaverryan
authored andcommitted
More concrete explanation of validation groups
1 parent 144e5af commit 81728b6

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

book/forms.rst

+32-3
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ to an array callback::
526526

527527
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
528528

529+
// ...
529530
public function setDefaultOptions(OptionsResolverInterface $resolver)
530531
{
531532
$resolver->setDefaults(array(
@@ -541,23 +542,51 @@ This will call the static method ``determineValidationGroups()`` on the
541542
The Form object is passed as an argument to that method (see next example).
542543
You can also define whole logic inline by using a ``Closure``::
543544

545+
use Acme\AcmeBundle\Entity\Client;
544546
use Symfony\Component\Form\FormInterface;
545547
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
546548

549+
// ...
547550
public function setDefaultOptions(OptionsResolverInterface $resolver)
548551
{
549552
$resolver->setDefaults(array(
550553
'validation_groups' => function(FormInterface $form) {
551554
$data = $form->getData();
552-
if (Entity\Client::TYPE_PERSON == $data->getType()) {
555+
if (Client::TYPE_PERSON == $data->getType()) {
553556
return array('person');
554-
} else {
555-
return array('company');
556557
}
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');
557583
},
558584
));
559585
}
560586

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+
561590
.. index::
562591
single: Forms; Validation groups based on clicked button
563592

book/validation.rst

+28-1
Original file line numberDiff line numberDiff line change
@@ -814,11 +814,38 @@ With this configuration, there are three validation groups:
814814
that belong to no other group.
815815

816816
``User``
817-
Equivalent to all constraints of the ``User`` object in the ``Default`` group.
817+
Equivalent to all constraints of the ``User`` object in the ``Default``
818+
group. This is always the name of the class. The difference between this
819+
and ``Default`` is explained below.
818820

819821
``registration``
820822
Contains the constraints on the ``email`` and ``password`` fields only.
821823

824+
Constraints in the ``Default`` group of a class are the constraints that have either no
825+
explicit group configured or that are configured to a group equal to the class name or
826+
the string ``Default``.
827+
828+
.. caution::
829+
830+
When validating *just* the User object, there is no difference between the ``Default`` group
831+
and the ``User`` group. But, there is a difference if ``User`` has embedded objects. For example,
832+
imagine ``User`` has an ``address`` property that contains some ``Address`` object and that
833+
you've added the :doc:`/reference/constraints/valid` constraint to this property so that it's
834+
validated when you validate the ``User`` object.
835+
836+
If you validate ``User`` using the ``Default`` group, then any constraints on the ``Address``
837+
class that are in the ``Default`` group *will* be used. But, if you validate ``User`` using the
838+
``User`` validation group, then only constraints on the ``Address`` class with the ``User``
839+
group will be validated.
840+
841+
In other words, the ``Default`` group and the class name group (e.g. ``User``) are identical,
842+
except when the class is embedded in another object that's actually the one being validated.
843+
844+
In case you have inheritance in your data model and you validate with the class name of
845+
the subclass in the subclass and in the baseclass all constraints in the default group
846+
will be validated. If you use the name of the baseclass only the constraints in the base
847+
class will be validated.
848+
822849
To tell the validator to use a specific group, pass one or more group names
823850
as the second argument to the ``validate()`` method::
824851

0 commit comments

Comments
 (0)