Browse Source

[Validator] Fixed: Collections annotated with @Valid may contain scalar values. These values are ignored by the GraphWalker

Bernhard Schussek 14 years ago
parent
commit
1a34743990

+ 4 - 1
src/Symfony/Component/Validator/GraphWalker.php

@@ -142,7 +142,10 @@ class GraphWalker
 
             if ($traverse && (is_array($value) || $value instanceof \Traversable)) {
                 foreach ($value as $key => $element) {
-                    $this->walkReference($element, $group, $propertyPath.'['.$key.']', $traverse);
+                    // Ignore any scalar values in the collection
+                    if (is_object($element) || is_array($element)) {
+                        $this->walkReference($element, $group, $propertyPath.'['.$key.']', $traverse);
+                    }
                 }
             }
 

+ 18 - 0
tests/Symfony/Tests/Component/Validator/GraphWalkerTest.php

@@ -330,6 +330,24 @@ class GraphWalkerTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($violations, $this->walker->getViolations());
     }
 
+    public function testWalkCascadedPropertyDoesNotValidateNestedScalarValues()
+    {
+        // validate array when validating the property "reference"
+        $this->metadata->addPropertyConstraint('reference', new Valid());
+
+        $this->walker->walkPropertyValue(
+            $this->metadata,
+            'reference',
+            array('scalar', 'values'),
+            'Default',
+            'path'
+        );
+
+        $violations = new ConstraintViolationList();
+
+        $this->assertEquals($violations, $this->walker->getViolations());
+    }
+
     public function testWalkCascadedPropertyDoesNotValidateNullValues()
     {
         $this->metadata->addPropertyConstraint('reference', new Valid());