@@ -779,6 +779,9 @@ With this configuration, there are two validation groups:
779
779
780
780
* ``Default `` - contains the constraints not assigned to any other group;
781
781
782
+ * ``User `` - contains the constraints that belongs to group ``Default ``
783
+ (this group is useful for :ref: `book-validation-group-sequence `);
784
+
782
785
* ``registration `` - contains the constraints on the ``email `` and ``password ``
783
786
fields only.
784
787
@@ -787,13 +790,138 @@ as the second argument to the ``validate()`` method::
787
790
788
791
$errors = $validator->validate($author, array('registration'));
789
792
793
+ If no groups are specified, all constraints that belong in group ``Default ``
794
+ will be applied.
795
+
790
796
Of course, you'll usually work with validation indirectly through the form
791
797
library. For information on how to use validation groups inside forms, see
792
798
:ref: `book-forms-validation-groups `.
793
799
794
800
.. index ::
795
801
single: Validation; Validating raw values
796
802
803
+ .. _book-validation-group-sequence :
804
+
805
+ Group Sequence
806
+ --------------
807
+
808
+ In some cases, you want to validate your groups by steps. To do this, you can
809
+ use the ``GroupSequence `` feature. In the case, an object defines a group sequence,
810
+ and then the groups in the group sequence are validated in order.
811
+
812
+ .. tip ::
813
+
814
+ Group sequences cannot contain the group ``Default ``, as this would create
815
+ a loop. Instead, use the group ``{ClassName} `` (e.g. ``User ``) instead.
816
+
817
+ For example, suppose you have a ``User `` class and want to validate that the
818
+ username and the password are different only if all other validation passes
819
+ (in order to avoid multiple error messages).
820
+
821
+ .. configuration-block ::
822
+
823
+ .. code-block :: yaml
824
+
825
+ # src/Acme/BlogBundle/Resources/config/validation.yml
826
+ Acme\BlogBundle\Entity\User :
827
+ group_sequence :
828
+ - User
829
+ - Strict
830
+ getters :
831
+ passwordLegal :
832
+ - " True " :
833
+ message : " The password cannot match your username"
834
+ groups : [Strict]
835
+ properties :
836
+ username :
837
+ - NotBlank : ~
838
+ password :
839
+ - NotBlank : ~
840
+
841
+ .. code-block :: php-annotations
842
+
843
+ // src/Acme/BlogBundle/Entity/User.php
844
+ namespace Acme\BlogBundle\Entity;
845
+
846
+ use Symfony\Component\Security\Core\User\UserInterface;
847
+ use Symfony\Component\Validator\Constraints as Assert;
848
+
849
+ /**
850
+ * @Assert\GroupSequence({"Strict", "User"})
851
+ */
852
+ class User implements UserInterface
853
+ {
854
+ /**
855
+ * @Assert\NotBlank
856
+ */
857
+ private $username;
858
+
859
+ /**
860
+ * @Assert\NotBlank
861
+ */
862
+ private $password;
863
+
864
+ /**
865
+ * @Assert\True(message="The password cannot match your username", groups={"Strict"})
866
+ */
867
+ public function isPasswordLegal()
868
+ {
869
+ return ($this->username !== $this->password);
870
+ }
871
+ }
872
+
873
+ .. code-block :: xml
874
+
875
+ <!-- src/Acme/BlogBundle/Resources/config/validation.xml -->
876
+ <class name =" Acme\BlogBundle\Entity\User" >
877
+ <property name =" username" >
878
+ <constraint name =" NotBlank" />
879
+ </property >
880
+ <property name =" password" >
881
+ <constraint name =" NotBlank" />
882
+ </property >
883
+ <getter property =" passwordLegal" >
884
+ <constraint name =" True" >
885
+ <option name =" message" >The password cannot match your username</option >
886
+ <option name =" groups" >
887
+ <value >Strict</value >
888
+ </option >
889
+ </constraint >
890
+ </getter >
891
+ <group-sequence >
892
+ <value >User</value >
893
+ <value >Strict</value >
894
+ </group-sequence >
895
+ </class >
896
+
897
+ .. code-block :: php
898
+
899
+ // src/Acme/BlogBundle/Entity/User.php
900
+ namespace Acme\BlogBundle\Entity;
901
+
902
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
903
+ use Symfony\Component\Validator\Constraints as Assert;
904
+
905
+ class User
906
+ {
907
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
908
+ {
909
+ $metadata->addPropertyConstraint('username', new Assert\NotBlank());
910
+ $metadata->addPropertyConstraint('password', new Assert\NotBlank());
911
+
912
+ $metadata->addGetterConstraint('passwordLegal', new Assert\True(array(
913
+ 'message' => 'The password cannot match your first name',
914
+ 'groups' => array('Strict'),
915
+ )));
916
+
917
+ $metadata->setGroupSequence(array('User', 'Strict'));
918
+ }
919
+ }
920
+
921
+ In this example, it will first validate all constraints in the group ``User ``
922
+ (which is the same as the ``Default `` group). Only if all constraints in
923
+ that group are valid, the second group, ``Strict ``, will be validated.
924
+
797
925
.. _book-validation-raw-values :
798
926
799
927
Validating Values and Arrays
0 commit comments