From fc38e2b438a1adbfc1c1154c57da93e89fd355c2 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Sun, 27 May 2012 14:53:01 +0200 Subject: [PATCH] [Form] Fixed mapping of violations with empty paths to the root form --- .../ViolationMapper/ViolationMapper.php | 18 +++++++++++++++--- .../ViolationMapper/ViolationMapperTest.php | 3 +++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php index ddf061d454..6e41c8f815 100644 --- a/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php +++ b/src/Symfony/Component/Form/Extension/Validator/ViolationMapper/ViolationMapper.php @@ -52,10 +52,22 @@ class ViolationMapper implements ViolationMapperInterface { $this->allowNonSynchronized = $allowNonSynchronized; - $violationPath = new ViolationPath($violation->getPropertyPath()); - $relativePath = $this->reconstructPath($violationPath, $form); + $violationPath = null; + $relativePath = null; $match = false; + // Don't create a ViolationPath instance for empty property paths + if (strlen($violation->getPropertyPath()) > 0) { + $violationPath = new ViolationPath($violation->getPropertyPath()); + $relativePath = $this->reconstructPath($violationPath, $form); + } + + // This case happens if the violation path is empty and thus + // the violation should be mapped to the root form + if (null === $violationPath) { + $this->scope = $form; + } + // In general, mapping happens from the root form to the leaf forms // First, the rules of the root form are applied to determine // the subsequent descendant. The rules of this descendant are then @@ -86,7 +98,7 @@ class ViolationMapper implements ViolationMapperInterface // This case happens if an error happened in the data under a // virtual form that does not match any of the children of // the virtual form. - if (!$match) { + if (null !== $violationPath && !$match) { // If we could not map the error to anything more specific // than the root element, map it to the innermost directly // mapped form of the violation path 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 30cab31f09..5b29110937 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php @@ -221,6 +221,9 @@ class ViolationMapperTest extends \PHPUnit_Framework_TestCase return array( // mapping target, child name, its property path, grand child name, its property path, violation path + array(self::LEVEL_0, 'address', 'address', 'street', 'street', ''), + array(self::LEVEL_0, 'address', 'address', 'street', 'street', 'data'), + array(self::LEVEL_2, 'address', 'address', 'street', 'street', 'children[address].children[street].data'), array(self::LEVEL_2, 'address', 'address', 'street', 'street', 'children[address].children[street].data.prop'), array(self::LEVEL_2, 'address', 'address', 'street', 'street', 'children[address].data.street'),