From 5e2b161f8ff70110e55a6de5a0265f8e136a7f03 Mon Sep 17 00:00:00 2001 From: Clement Gautier Date: Mon, 20 May 2013 11:25:56 +0200 Subject: [PATCH] Added documentation for GroupSequence --- book/validation.rst | 126 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/book/validation.rst b/book/validation.rst index 0a249c9e99d..ebe84139c60 100644 --- a/book/validation.rst +++ b/book/validation.rst @@ -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`); + * ``registration`` - contains the constraints on the ``email`` and ``password`` fields only. @@ -779,6 +782,9 @@ 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`. @@ -786,6 +792,126 @@ library. For information on how to use validation groups inside forms, see .. 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 + + + + + + + + + + + + + + + + + User + Strict + + + + .. 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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy