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