Skip to content

Commit

Permalink
[Form] readPropertyPath should return null instead of empty array
Browse files Browse the repository at this point in the history
When reading the last bit of a property path mapped to a missing array index, the method would initialize the value to an empty array.  This makes sense for cases where readPropertyPath would again be called recursively, but not when the value would be immediately returned (null would be preferable in that case).

For example, we have an object with a property called "options" that's an array of arbitrary key/value pairs.  That "options" property (and getOptions()) maps directly to a FieldGroup within the Form for this object.  That FieldGroup contains multiple TextFields for a few expected keys in the array.  As-is, if those keys were not defined, the default data set for those TextFields could end up being "Array" (string representation of an empty array).  If readPropertyPath instead returns null for this case, the default data would be transformed into an empty string.
  • Loading branch information
Jeremy Mikola authored and fabpot committed Oct 13, 2010
1 parent 2b8dfe1 commit df9ef79
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/PropertyPath.php
Expand Up @@ -237,7 +237,7 @@ protected function readPropertyPath(&$objectOrArray, $currentIndex)
// http://bugs.php.net/bug.php?id=52133
else {
if (!array_key_exists($property, $objectOrArray)) {
$objectOrArray[$property] = array();
$objectOrArray[$property] = $currentIndex + 1 < $this->length ? array() : null;
}

$value =& $objectOrArray[$property];
Expand Down
9 changes: 9 additions & 0 deletions tests/Symfony/Tests/Component/Form/PropertyPathTest.php
Expand Up @@ -37,6 +37,15 @@ public function testGetValueReadsArrayWithCustomPropertyPath()
$this->assertEquals('Bernhard', $path->getValue($array));
}

public function testGetValueReadsArrayWithMissingIndexForCustomPropertyPath()
{
$array = array('child' => array('index' => array()));

$path = new PropertyPath('child[index].firstName');

$this->assertNull($path->getValue($array));
}

public function testGetValueReadsProperty()
{
$object = new Author();
Expand Down

0 comments on commit df9ef79

Please sign in to comment.