Skip to content

[WIP] Added documentation for GroupSequence #2647

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2013
Merged
Changes from all 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
126 changes: 126 additions & 0 deletions book/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,9 @@ With this configuration, there are two validation groups:

* ``Default`` - contains the constraints not assigned to any other group;

* ``User`` - contains the constraints that belongs to group ``Default``
(this group is usefull for :ref:`book-validation-group-sequence`);
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong. It does not correspond to the example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why ? The entity class in the example is User so the validation group is named User right ?


* ``registration`` - contains the constraints on the ``email`` and ``password``
fields only.

Expand All @@ -779,13 +782,136 @@ as the second argument to the ``validate()`` method::

$errors = $validator->validate($author, array('registration'));

If no groups are specified, all constraints that belong in group ``Default``
will be applied.

Of course, you'll usually work with validation indirectly through the form
library. For information on how to use validation groups inside forms, see
:ref:`book-forms-validation-groups`.

.. index::
single: Validation; Validating raw values

.. _book-validation-group-sequence:

Group Sequence
--------------

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

Group sequences cannot contain the group ``Default``, this would create a
cycle, but need to contain the group ``{ClassName}`` instead.

For example, suppose you have a ``User`` class and want to validate that the
username and the password are different only if all other validations passes
(in order to avoid multiple error messages).

.. configuration-block::

.. code-block:: yaml

# src/Acme/BlogBundle/Resources/config/validation.yml
Acme\BlogBundle\Entity\User:
group_sequence:
- User
- Strict
getters:
passwordLegal:
- "True":
message: "The password cannot match your username"
groups: [Strict]
properties:
username:
- NotBlank: ~
password:
- NotBlank: ~

.. code-block:: php-annotations

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

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @Assert\GroupSequence({"Strict", "User"})
*/
class User implements UserInterface
{
/**
* @Assert\NotBlank
*/
private $username;

/**
* @Assert\NotBlank
*/
private $password;

/**
* @Assert\True(message="The password cannot match your username", groups={"Strict"})
*/
public function isPasswordLegal()
{
return ($this->username !== $this->password);
}
}

.. code-block:: xml

<!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
<class name="Acme\BlogBundle\Entity\User">
<property name="username">
<constraint name="NotBlank" />
</property>
<property name="password">
<constraint name="NotBlank" />
</property>
<getter property="passwordLegal">
<constraint name="True">
<option name="message">The password cannot match your username</option>
<option name="groups">
<value>Strict</value>
</option>
</constraint>
</getter>
<group-sequence>
<value>User</value>
<value>Strict</value>
</group-sequence>
</class>

.. code-block:: php

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

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

class User
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('username', new Assert\NotBlank());
$metadata->addPropertyConstraint('password', new Assert\NotBlank());

$metadata->addGetterConstraint('passwordLegal', new Assert\True(array(
'message' => 'The password cannot match your first name',
'groups' => array('Strict'),
)));

$metadata->setGroupSequence(array('User', 'Strict'));
}
}

In this example, it will first validate all constraints in group ``User``
(eg. ``Default``). Only if all constraints in that group are valid, the second
group, ``Strict``, will be validated.

.. _book-validation-raw-values:

Validating Values and Arrays
Expand Down