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