From 09a50c3c55257edb95c102cb435e353f0b118208 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 7 Feb 2011 18:36:29 +0100 Subject: [PATCH] [Form] Fixed: "by_reference" option is ignored if reading from/writing to an array --- src/Symfony/Component/Form/Form.php | 2 +- .../Symfony/Tests/Component/Form/FormTest.php | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index ead59ddaa2..fd1f1d11a3 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -895,7 +895,7 @@ class Form extends Field implements \IteratorAggregate, FormInterface // Don't update parent if data is a composite type (object or array) // and "by_reference" option is true, because then we expect that // we are working with a reference to the parent's data - if (!is_object($data) || !$this->getOption('by_reference')) { + if (!is_object($data) || !is_object($objectOrArray) || !$this->getOption('by_reference')) { parent::writeProperty($objectOrArray); } } diff --git a/tests/Symfony/Tests/Component/Form/FormTest.php b/tests/Symfony/Tests/Component/Form/FormTest.php index b56f7f8e52..711274a14d 100644 --- a/tests/Symfony/Tests/Component/Form/FormTest.php +++ b/tests/Symfony/Tests/Component/Form/FormTest.php @@ -1258,6 +1258,29 @@ class FormTest extends \PHPUnit_Framework_TestCase $this->assertEquals('foobar', $author->getReferenceCopy()); } + public function testSubformAlwaysInsertsIntoArrays() + { + $ref1 = new Author(); + $ref2 = new Author(); + $author = array('referenceCopy' => $ref1); + + $form = new Form('author', array('validator' => $this->createMockValidator())); + $form->setData($author); + $refForm = new FormTest_FormThatReturns('referenceCopy'); + $refForm->setReturnValue($ref2); + $form->add($refForm); + + $form->bind($this->createPostRequest(array( + 'author' => array( + 'referenceCopy' => array(), // doesn't matter actually + ) + ))); + + // the new reference was inserted into the array + $author = $form->getData(); + $this->assertSame($ref2, $author['referenceCopy']); + } + /** * Create a group containing two fields, "visibleField" and "hiddenField" *