Skip to content

Commit 7c49c2f

Browse files
committed
Added docs about groupsequenceproviders
1 parent e0c16d3 commit 7c49c2f

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

book/validation.rst

+127
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,133 @@ In this example, it will first validate all constraints in the group ``User``
924924
(which is the same as the ``Default`` group). Only if all constraints in
925925
that group are valid, the second group, ``Strict``, will be validated.
926926

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

9291056
Validating Values and Arrays

0 commit comments

Comments
 (0)