From 7c866c08806303746c77e1ce2c273f7c6a4d2a4c Mon Sep 17 00:00:00 2001 From: "Issei.M" Date: Wed, 11 May 2016 01:50:58 +0900 Subject: [PATCH 1/2] [Form] fixed ViolationMapper bug --- .../Validator/ViolationMapper/ViolationMapper.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php index d3ba9cf7bf23b..304ec17e1a493 100644 --- a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php +++ b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php @@ -162,9 +162,7 @@ private function matchChild(FormInterface $form, PropertyPathIteratorInterface $ } } - $children = iterator_to_array(new \RecursiveIteratorIterator( - new InheritDataAwareIterator($form) - )); + $children = iterator_to_array(new \RecursiveIteratorIterator(new InheritDataAwareIterator($form)), false); while ($it->valid()) { if ($it->isIndex()) { @@ -189,7 +187,7 @@ private function matchChild(FormInterface $form, PropertyPathIteratorInterface $ } /** @var FormInterface $child */ - foreach ($children as $key => $child) { + foreach ($children as $i => $child) { $childPath = (string) $child->getPropertyPath(); if ($childPath === $chunk) { $target = $child; @@ -198,7 +196,7 @@ private function matchChild(FormInterface $form, PropertyPathIteratorInterface $ continue; } - unset($children[$key]); + unset($children[$i]); } $it->next(); From cf520b5f528ffc16d4e429631ea8d68067626f19 Mon Sep 17 00:00:00 2001 From: "Issei.M" Date: Wed, 11 May 2016 13:23:02 +0900 Subject: [PATCH 2/2] [Form] fixed ViolationMapperTest --- .../ViolationMapper/ViolationMapperTest.php | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php index a099e104595ff..74da4aee1c281 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php @@ -1542,24 +1542,39 @@ public function testErrorMappingForFormInheritingParentData($target, $childName, public function testBacktrackIfSeveralSubFormsWithSamePropertyPath() { - $violation = $this->getConstraintViolation('data.address[street]'); $parent = $this->getForm('parent'); $child1 = $this->getForm('subform1', 'address'); $child2 = $this->getForm('subform2', 'address'); - $grandChild = $this->getForm('street'); + $child3 = $this->getForm('subform3', null, null, array(), true); + $child4 = $this->getForm('subform4', null, null, array(), true); + $grandChild1 = $this->getForm('street'); + $grandChild2 = $this->getForm('street', '[sub_address1_street]'); + $grandChild3 = $this->getForm('street', '[sub_address2_street]'); $parent->add($child1); $parent->add($child2); - $child2->add($grandChild); + $parent->add($child3); + $parent->add($child4); + $child2->add($grandChild1); + $child3->add($grandChild2); + $child4->add($grandChild3); $parent->submit(array()); - $this->mapper->mapViolation($violation, $parent); + $violation1 = $this->getConstraintViolation('data.address[street]'); + $violation2 = $this->getConstraintViolation('data[sub_address1_street]'); + $violation3 = $this->getConstraintViolation('data[sub_address2_street]'); + $this->mapper->mapViolation($violation1, $parent); + $this->mapper->mapViolation($violation2, $parent); + $this->mapper->mapViolation($violation3, $parent); - // The error occurred on the child of the second form with the same path $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); $this->assertCount(0, $child1->getErrors(), $child1->getName().' should not have an error, but has one'); $this->assertCount(0, $child2->getErrors(), $child2->getName().' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError($violation, $grandChild)), iterator_to_array($grandChild->getErrors()), $grandChild->getName().' should have an error, but has none'); + $this->assertCount(0, $child3->getErrors(), $child3->getName().' should not have an error, but has one'); + $this->assertCount(0, $child4->getErrors(), $child4->getName().' should not have an error, but has one'); + $this->assertEquals(array($this->getFormError($violation1, $grandChild1)), iterator_to_array($grandChild1->getErrors()), $grandChild1->getName().' should have an error, but has none'); + $this->assertEquals(array($this->getFormError($violation2, $grandChild2)), iterator_to_array($grandChild2->getErrors()), $grandChild2->getName().' should have an error, but has none'); + $this->assertEquals(array($this->getFormError($violation3, $grandChild3)), iterator_to_array($grandChild3->getErrors()), $grandChild3->getName().' should have an error, but has none'); } }