浏览代码

[Form] Fixed: "by_reference" option is ignored if reading from/writing to an array

Bernhard Schussek 14 年之前
父节点
当前提交
09a50c3c55
共有 2 个文件被更改,包括 24 次插入1 次删除
  1. 1 1
      src/Symfony/Component/Form/Form.php
  2. 23 0
      tests/Symfony/Tests/Component/Form/FormTest.php

+ 1 - 1
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);
         }
     }

+ 23 - 0
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"
      *