@@ -932,6 +932,133 @@ In this example, it will first validate all constraints in the group ``User``
932932(which is the same as the ``Default `` group). Only if all constraints in
933933that group are valid, the second group, ``Strict ``, will be validated.
934934
935+ Group Sequence Providers
936+ ~~~~~~~~~~~~~~~~~~~~~~~~
937+
938+ Imagine a ``User `` entity which can be a normal user or a premium user. When
939+ it's a premium user, some extra constraints should be added to the user entity
940+ (e.g. the credit card details). To dynamically determine which groups should
941+ be activated, you can create a Group Sequence Provider. First, create the
942+ entity and a new constraint group called ``Premium ``:
943+
944+ ..configuration-block ::
945+
946+ ..code-block ::php-annotations
947+
948+ // src/Acme/DemoBundle/Entity/User.php
949+ namespace Acme\DemoBundle\Entity;
950+
951+ use Symfony\Component\Validator\Constraints as Assert;
952+
953+ class User
954+ {
955+ // ...
956+
957+ /**
958+ * @Assert\NotBlank()
959+ */
960+ private $name;
961+
962+ /**
963+ * @Assert\CardScheme(
964+ * schemes={"VISA"},
965+ * groups={"Premium"},
966+ * )
967+ private $creditCard;
968+ }
969+
970+ ..code-block ::yaml
971+
972+ # src/Acme/DemoBundle/Resources/config/validation.yml
973+ Acme\DemoBundle\Entity\User :
974+ properties :
975+ name :
976+ -NotBlank
977+ creditCard :
978+ -CardScheme
979+ schemes :[VISA]
980+ groups :[Premium]
981+
982+ ..code-block ::xml
983+
984+ <!-- src/Acme/DemoBundle/Resources/config/validation.xml-->
985+ <class name =" Acme\DemoBundle\Entity\User" >
986+ <property name =" name" >
987+ <constraint name =" NotBlank" />
988+ </property >
989+
990+ <property name =" creditCard" >
991+ <constraint name =" CardScheme" >
992+ <option name =" schemes" >
993+ <value >VISA</value >
994+ </option >
995+ <option name =" groups" >
996+ <value >Premium</value >
997+ </option >
998+ </constraint >
999+ </property >
1000+ </class >
1001+
1002+ ..code-block ::php
1003+
1004+ // src/Acme/DemoBundle/Entity/User.php
1005+ namespace Acme\DemoBundle\Entity;
1006+
1007+ use Symfony\Component\Validator\Constraints as Assert;
1008+ use Symfony\Component\Validator\Mapping\ClassMetadata;
1009+
1010+ class User
1011+ {
1012+ private $name;
1013+ private $creditCard;
1014+
1015+ // ...
1016+
1017+ public static function loadValidatorMetadata(ClassMetadata $metadata)
1018+ {
1019+ $metadata->addPropertyConstraint('name', new Assert\NotBlank());
1020+ $metadata->addPropertyConstraint('creditCard', new Assert\CardScheme(
1021+ 'schemes' => array('VISA'),
1022+ 'groups' => array('Premium'),
1023+ ));
1024+ }
1025+ }
1026+
1027+ Now, let this class implement
1028+ :class: `Symfony\\ Componet\\ Validation\\ GroupSequenceProviderInterface ` and
1029+ implement a method called
1030+ :method: `Symfony\\ Componet\\ Validation\\ GroupSequenceProviderInterface::getGroupSequence `,
1031+ which returns an array of groups to use and add the
1032+ ``@Assert\GroupSequencdeProvider `` annotation to the class. Imagine a method
1033+ ``isPremium `` returns true if the user is a premium member. Your method looks
1034+ like this::
1035+
1036+ // src/Acme/DemoBundle/Entity/User.php
1037+ namespace Acme\DemoBundle\Entity;
1038+
1039+ // ...
1040+ use Symfony\Component\Validation\GroupSequenceProviderInterface;
1041+
1042+ /**
1043+ * @Assert\GroupSequenceProvider
1044+ * ...
1045+ */
1046+ class User
1047+ {
1048+ // ...
1049+
1050+ public function getGroupSequence()
1051+ {
1052+ $groups = array('User');
1053+
1054+ if ($this->isPremium()) {
1055+ $groups[] = 'Premium';
1056+ }
1057+
1058+ return $groups;
1059+ }
1060+ }
1061+
9351062.. _book-validation-raw-values :
9361063
9371064Validating Values and Arrays