Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
Added docs about GroupSequenceProviders#2766
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -924,6 +924,133 @@ In this example, it will first validate all constraints in the group ``User`` | ||
| (which is the same as the ``Default`` group). Only if all constraints in | ||
| that group are valid, the second group, ``Strict``, will be validated. | ||
| Group Sequence Providers | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| Imagine a ``User`` entity which can be a normal user or a premium user. When | ||
| it's a premium user, some extra constraints should be added to the user entity | ||
| (e.g. the credit card details). To dynamically determine which groups should | ||
| be activated, you can create a Group Sequence Provider. First, create the | ||
| entity and a new constraint group called ``Premium``: | ||
| ..configuration-block:: | ||
| ..code-block::php-annotations | ||
| // src/Acme/DemoBundle/Entity/User.php | ||
| namespace Acme\DemoBundle\Entity; | ||
| use Symfony\Component\Validator\Constraints as Assert; | ||
| class User | ||
| { | ||
| // ... | ||
| /** | ||
| * @Assert\NotBlank() | ||
| */ | ||
| private $name; | ||
| /** | ||
| * @Assert\CardScheme( | ||
| * schemes={"VISA"}, | ||
| * groups={"Premium"}, | ||
| * ) | ||
| private $creditCard; | ||
| } | ||
| ..code-block::yaml | ||
| # src/Acme/DemoBundle/Resources/config/validation.yml | ||
| Acme\DemoBundle\Entity\User: | ||
| properties: | ||
| name: | ||
| -NotBlank | ||
| creditCard: | ||
| -CardScheme | ||
| schemes:[VISA] | ||
| groups:[Premium] | ||
| ..code-block::xml | ||
| <!-- src/Acme/DemoBundle/Resources/config/validation.xml--> | ||
| <classname="Acme\DemoBundle\Entity\User"> | ||
| <propertyname="name"> | ||
| <constraintname="NotBlank" /> | ||
| </property> | ||
| <propertyname="creditCard"> | ||
| <constraintname="CardScheme"> | ||
| <optionname="schemes"> | ||
| <value>VISA</value> | ||
| </option> | ||
| <optionname="groups"> | ||
| <value>Premium</value> | ||
| </option> | ||
| </constraint> | ||
| </property> | ||
| </class> | ||
| ..code-block::php | ||
| // src/Acme/DemoBundle/Entity/User.php | ||
| namespace Acme\DemoBundle\Entity; | ||
| use Symfony\Component\Validator\Constraints as Assert; | ||
| use Symfony\Component\Validator\Mapping\ClassMetadata; | ||
| class User | ||
| { | ||
| private $name; | ||
| private $creditCard; | ||
| // ... | ||
| public static function loadValidatorMetadata(ClassMetadata $metadata) | ||
| { | ||
| $metadata->addPropertyConstraint('name', new Assert\NotBlank()); | ||
| $metadata->addPropertyConstraint('creditCard', new Assert\CardScheme( | ||
| 'schemes' => array('VISA'), | ||
| 'groups' => array('Premium'), | ||
| )); | ||
| } | ||
| } | ||
| Now, let this class implement | ||
| :class:`Symfony\\Componet\\Validation\\GroupSequenceProviderInterface` and | ||
| implement a method called | ||
| :method:`Symfony\\Componet\\Validation\\GroupSequenceProviderInterface::getGroupSequence`, | ||
| which returns an array of groups to use and add the | ||
| ``@Assert\GroupSequencdeProvider`` annotation to the class. Imagine a method | ||
| ``isPremium`` returns true if the user is a premium member. Your method looks | ||
| like this:: | ||
| // src/Acme/DemoBundle/Entity/User.php | ||
| namespace Acme\DemoBundle\Entity; | ||
| // ... | ||
| use Symfony\Component\Validation\GroupSequenceProviderInterface; | ||
| /** | ||
| * @Assert\GroupSequenceProvider | ||
| * ... | ||
| */ | ||
| class User | ||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. does this work without implementing Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. being a sequence provider without implementing the interface ? no. the interface is what makes you a sequence provider MemberAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I forgot it. Will change it when I can switch branches | ||
| { | ||
| // ... | ||
| public function getGroupSequence() | ||
| { | ||
| $groups = array('User'); | ||
| if ($this->isPremium()) { | ||
| $groups[] = 'Premium'; | ||
| } | ||
| return $groups; | ||
| } | ||
| } | ||
| .. _book-validation-raw-values: | ||
| Validating Values and Arrays | ||