Skip to content

Commit

Permalink
feature #3803 [Book][Validation] configuration examples for the Group…
Browse files Browse the repository at this point in the history
…SequenceProvider (xabbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[Book][Validation] configuration examples for the GroupSequenceProvider

| Q             | A
| ------------- | ---
| Doc fix?      | yes
| New docs?     | no
| Applies to    | all
| Fixed tickets |

This pull requests adds examples in different formats for the GroupSequenceProvider feature of the Validator component (it's the completion of #3369).

Commits
-------

bfd2af4 [Book][Validation] configuration examples for the GroupSequenceProvider
  • Loading branch information
weaverryan committed Apr 28, 2014
2 parents 55442b5 + bfd2af4 commit 322972e
Showing 1 changed file with 63 additions and 10 deletions.
73 changes: 63 additions & 10 deletions book/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -839,8 +839,8 @@ Group Sequence
--------------

In some cases, you want to validate your groups by steps. To do this, you can
use the ``GroupSequence`` feature. In this case, an object defines a group sequence
, which determines the order groups should be validated.
use the ``GroupSequence`` feature. In this case, an object defines a group
sequence, which determines the order groups should be validated.

For example, suppose you have a ``User`` class and want to validate that the
username and the password are different only if all other validation passes
Expand Down Expand Up @@ -1082,21 +1082,14 @@ Now, change the ``User`` class to implement
:class:`Symfony\\Component\\Validator\\GroupSequenceProviderInterface` and
add the
:method:`Symfony\\Component\\Validator\\GroupSequenceProviderInterface::getGroupSequence`,
which should return an array of groups to use. Also, add the
``@Assert\GroupSequenceProvider`` annotation to the class (or ``group_sequence_provider: true`` to the YAML). If you imagine
that a method called ``isPremium`` returns true if the user is a premium member,
then your code might look like this::
which should return an array of groups to use::

// src/Acme/DemoBundle/Entity/User.php
namespace Acme\DemoBundle\Entity;

// ...
use Symfony\Component\Validator\GroupSequenceProviderInterface;

/**
* @Assert\GroupSequenceProvider
* ...
*/
class User implements GroupSequenceProviderInterface
{
// ...
Expand All @@ -1113,6 +1106,66 @@ then your code might look like this::
}
}

At last, you have to notify the Validator component that your ``User`` class
provides a sequence of groups to be validated:

.. configuration-block::

.. code-block:: yaml

# src/Acme/DemoBundle/Resources/config/validation.yml
Acme\DemoBundle\Entity\User:
group_sequence_provider: ~

.. code-block:: php-annotations

// src/Acme/DemoBundle/Entity/User.php
namespace Acme\DemoBundle\Entity;

// ...

/**
* @Assert\GroupSequenceProvider
*/
class User implements GroupSequenceProviderInterface
{
// ...
}

.. code-block:: xml

<!-- src/Acme/DemoBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping
http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"
>
<class name="Acme\DemoBundle\Entity\User">
<group-sequence-provider />
<!-- ... -->
</class>
</constraint-mapping>

.. code-block:: php

// src/Acme/DemoBundle/Entity/User.php
namespace Acme\DemoBundle\Entity;

// ...
use Symfony\Component\Validator\Mapping\ClassMetadata;

class User implements GroupSequenceProviderInterface
{
// ...

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->setGroupSequenceProvider(true);
// ...
}
}

.. _book-validation-raw-values:

Validating Values and Arrays
Expand Down

0 comments on commit 322972e

Please sign in to comment.