@@ -839,8 +839,8 @@ Group Sequence
839
839
--------------
840
840
841
841
In some cases, you want to validate your groups by steps. To do this, you can
842
- use the ``GroupSequence `` feature. In this case, an object defines a group sequence
843
- , which determines the order groups should be validated.
842
+ use the ``GroupSequence `` feature. In this case, an object defines a group
843
+ sequence , which determines the order groups should be validated.
844
844
845
845
For example, suppose you have a ``User `` class and want to validate that the
846
846
username and the password are different only if all other validation passes
@@ -1082,21 +1082,14 @@ Now, change the ``User`` class to implement
1082
1082
:class: `Symfony\\ Component\\ Validator\\ GroupSequenceProviderInterface ` and
1083
1083
add the
1084
1084
:method: `Symfony\\ Component\\ Validator\\ GroupSequenceProviderInterface::getGroupSequence `,
1085
- which should return an array of groups to use. Also, add the
1086
- ``@Assert\GroupSequenceProvider `` annotation to the class (or ``group_sequence_provider: true `` to the YAML). If you imagine
1087
- that a method called ``isPremium `` returns true if the user is a premium member,
1088
- then your code might look like this::
1085
+ which should return an array of groups to use::
1089
1086
1090
1087
// src/Acme/DemoBundle/Entity/User.php
1091
1088
namespace Acme\DemoBundle\Entity;
1092
1089
1093
1090
// ...
1094
1091
use Symfony\Component\Validator\GroupSequenceProviderInterface;
1095
1092
1096
- /**
1097
- * @Assert\GroupSequenceProvider
1098
- * ...
1099
- */
1100
1093
class User implements GroupSequenceProviderInterface
1101
1094
{
1102
1095
// ...
@@ -1113,6 +1106,66 @@ then your code might look like this::
1113
1106
}
1114
1107
}
1115
1108
1109
+ At last, you have to notify the Validator component that your ``User `` class
1110
+ provides a sequence of groups to be validated:
1111
+
1112
+ .. configuration-block ::
1113
+
1114
+ .. code-block :: yaml
1115
+
1116
+ # src/Acme/DemoBundle/Resources/config/validation.yml
1117
+ Acme\DemoBundle\Entity\User :
1118
+ group_sequence_provider : ~
1119
+
1120
+ .. code-block :: php-annotations
1121
+
1122
+ // src/Acme/DemoBundle/Entity/User.php
1123
+ namespace Acme\DemoBundle\Entity;
1124
+
1125
+ // ...
1126
+
1127
+ /**
1128
+ * @Assert\GroupSequenceProvider
1129
+ */
1130
+ class User implements GroupSequenceProviderInterface
1131
+ {
1132
+ // ...
1133
+ }
1134
+
1135
+ .. code-block :: xml
1136
+
1137
+ <!-- src/Acme/DemoBundle/Resources/config/validation.xml -->
1138
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1139
+ <constraint-mapping xmlns =" http://symfony.com/schema/dic/constraint-mapping"
1140
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1141
+ xsi : schemaLocation =" http://symfony.com/schema/dic/constraint-mapping
1142
+ http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"
1143
+ >
1144
+ <class name =" Acme\DemoBundle\Entity\User" >
1145
+ <group-sequence-provider />
1146
+ <!-- ... -->
1147
+ </class >
1148
+ </constraint-mapping >
1149
+
1150
+ .. code-block :: php
1151
+
1152
+ // src/Acme/DemoBundle/Entity/User.php
1153
+ namespace Acme\DemoBundle\Entity;
1154
+
1155
+ // ...
1156
+ use Symfony\Component\Validator\Mapping\ClassMetadata;
1157
+
1158
+ class User implements GroupSequenceProviderInterface
1159
+ {
1160
+ // ...
1161
+
1162
+ public static function loadValidatorMetadata(ClassMetadata $metadata)
1163
+ {
1164
+ $metadata->setGroupSequenceProvider(true);
1165
+ // ...
1166
+ }
1167
+ }
1168
+
1116
1169
.. _book-validation-raw-values :
1117
1170
1118
1171
Validating Values and Arrays
0 commit comments