@@ -48,6 +48,10 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
48
48
private $ validatorFactory ;
49
49
private $ objectInitializers ;
50
50
51
+ private $ validatedObjectsReferences = [];
52
+ private $ validatedConstraintsReferences = [];
53
+ private $ initializedObjectsReferences = [];
54
+
51
55
/**
52
56
* Creates a validator for the given context.
53
57
*
@@ -128,6 +132,8 @@ public function validate($value, $constraints = null, $groups = null)
128
132
$ this ->context ->setConstraint ($ previousConstraint );
129
133
}
130
134
135
+ $ this ->clearReferences ();
136
+
131
137
return $ this ;
132
138
}
133
139
@@ -145,6 +151,8 @@ public function validate($value, $constraints = null, $groups = null)
145
151
$ this ->context ->setNode ($ previousValue , $ previousObject , $ previousMetadata , $ previousPath );
146
152
$ this ->context ->setGroup ($ previousGroup );
147
153
154
+ $ this ->clearReferences ();
155
+
148
156
return $ this ;
149
157
}
150
158
@@ -161,6 +169,8 @@ public function validate($value, $constraints = null, $groups = null)
161
169
$ this ->context ->setNode ($ previousValue , $ previousObject , $ previousMetadata , $ previousPath );
162
170
$ this ->context ->setGroup ($ previousGroup );
163
171
172
+ $ this ->clearReferences ();
173
+
164
174
return $ this ;
165
175
}
166
176
@@ -208,6 +218,8 @@ public function validateProperty($object, $propertyName, $groups = null)
208
218
$ this ->context ->setNode ($ previousValue , $ previousObject , $ previousMetadata , $ previousPath );
209
219
$ this ->context ->setGroup ($ previousGroup );
210
220
221
+ $ this ->clearReferences ();
222
+
211
223
return $ this ;
212
224
}
213
225
@@ -261,6 +273,8 @@ public function validatePropertyValue($objectOrClass, $propertyName, $value, $gr
261
273
$ this ->context ->setNode ($ previousValue , $ previousObject , $ previousMetadata , $ previousPath );
262
274
$ this ->context ->setGroup ($ previousGroup );
263
275
276
+ $ this ->clearReferences ();
277
+
264
278
return $ this ;
265
279
}
266
280
@@ -442,6 +456,10 @@ private function validateClassNode($object, $cacheKey, ClassMetadataInterface $m
442
456
{
443
457
$ context ->setNode ($ object , $ object , $ metadata , $ propertyPath );
444
458
459
+ if (!isset ($ this ->initializedObjectsReferences [$ cacheKey ])) {
460
+ $ this ->initializedObjectsReferences [$ cacheKey ] = $ object ;
461
+ }
462
+
445
463
if (!$ context ->isObjectInitialized ($ cacheKey )) {
446
464
foreach ($ this ->objectInitializers as $ initializer ) {
447
465
$ initializer ->initialize ($ object );
@@ -457,7 +475,15 @@ private function validateClassNode($object, $cacheKey, ClassMetadataInterface $m
457
475
$ defaultOverridden = false ;
458
476
459
477
// Use the object hash for group sequences
460
- $ groupHash = \is_object ($ group ) ? spl_object_hash ($ group ) : $ group ;
478
+ if (\is_object ($ group )) {
479
+ $ groupHash = spl_object_hash ($ group );
480
+
481
+ if (!isset ($ this ->validatedObjectsReferences [$ groupHash ])) {
482
+ $ this ->validatedObjectsReferences [$ groupHash ] = $ group ;
483
+ }
484
+ } else {
485
+ $ groupHash = $ group ;
486
+ }
461
487
462
488
if ($ context ->isGroupValidated ($ cacheKey , $ groupHash )) {
463
489
// Skip this group when validating the properties and when
@@ -790,6 +816,10 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,
790
816
// that constraints belong to multiple validated groups
791
817
if (null !== $ cacheKey ) {
792
818
$ constraintHash = spl_object_hash ($ constraint );
819
+ if (!isset ($ this ->validatedConstraintsReferences [$ constraintHash ])) {
820
+ $ this ->validatedConstraintsReferences [$ constraintHash ] = $ constraint ;
821
+ }
822
+
793
823
// instanceof Valid: In case of using a Valid constraint with many groups
794
824
// it makes a reference object get validated by each group
795
825
if ($ constraint instanceof Composite || $ constraint instanceof Valid) {
@@ -815,4 +845,10 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,
815
845
$ validator ->validate ($ value , $ constraint );
816
846
}
817
847
}
848
+
849
+ private function clearReferences ()
850
+ {
851
+ $ this ->validatedObjectsReferences = [];
852
+ $ this ->validatedConstraintsReferences = [];
853
+ }
818
854
}
0 commit comments