Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
weaverryan merged 1 commit intosymfony:2.2fromwouterj:issue_2664
Jul 28, 2013
Merged
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletionsbook/validation.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

does this work without implementingGroupSequenceProviderInterface?

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
MemberAuthor

Choose a reason for hiding this comment

The 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
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp