From 50955a391959febfd5b8612b0a0473fbc771cf0a Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 3 Feb 2011 11:28:51 +0100 Subject: [PATCH] [Validator] Fixed PropertyPath to read array indices with special characters --- src/Symfony/Component/Form/PropertyPath.php | 4 ++-- .../Tests/Component/Form/PropertyPathTest.php | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Form/PropertyPath.php b/src/Symfony/Component/Form/PropertyPath.php index 40a256aef2..5eebc349f3 100644 --- a/src/Symfony/Component/Form/PropertyPath.php +++ b/src/Symfony/Component/Form/PropertyPath.php @@ -63,7 +63,7 @@ class PropertyPath implements \IteratorAggregate $remaining = $propertyPath; // first element is evaluated differently - no leading dot for properties - $pattern = '/^((\w+)|\[(\w+)\])(.*)/'; + $pattern = '/^((\w+)|\[([^\]]+)\])(.*)/'; while (preg_match($pattern, $remaining, $matches)) { if ($matches[2] !== '') { @@ -76,7 +76,7 @@ class PropertyPath implements \IteratorAggregate $position += strlen($matches[1]); $remaining = $matches[4]; - $pattern = '/^(\.(\w+)|\[(\w+)\])(.*)/'; + $pattern = '/^(\.(\w+)|\[([^\]]+)\])(.*)/'; } if (!empty($remaining)) { diff --git a/tests/Symfony/Tests/Component/Form/PropertyPathTest.php b/tests/Symfony/Tests/Component/Form/PropertyPathTest.php index 0508dca65b..eb949f9d0f 100644 --- a/tests/Symfony/Tests/Component/Form/PropertyPathTest.php +++ b/tests/Symfony/Tests/Component/Form/PropertyPathTest.php @@ -39,6 +39,24 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase $this->assertEquals('Bernhard', $path->getValue($array)); } + public function testGetValueReadsIndexWithSpecialChars() + { + $array = array('#!@$.' => 'Bernhard'); + + $path = new PropertyPath('[#!@$.]'); + + $this->assertEquals('Bernhard', $path->getValue($array)); + } + + public function testGetValueReadsNestedIndexWithSpecialChars() + { + $array = array('root' => array('#!@$.' => 'Bernhard')); + + $path = new PropertyPath('root[#!@$.]'); + + $this->assertEquals('Bernhard', $path->getValue($array)); + } + public function testGetValueReadsArrayWithCustomPropertyPath() { $array = array('child' => array('index' => array('firstName' => 'Bernhard')));