Ver Fonte

[Form] Fixed: "data_constructor" option is used even if "data_class" option is not set

Bernhard Schussek há 14 anos atrás
pai
commit
f51dafca3f

+ 8 - 7
src/Symfony/Component/Form/Form.php

@@ -112,10 +112,6 @@ class Form extends Field implements \IteratorAggregate, FormInterface
 
 
         if (isset($options['data_constructor'])) {
         if (isset($options['data_constructor'])) {
             $this->dataConstructor = $options['data_constructor'];
             $this->dataConstructor = $options['data_constructor'];
-        } else {
-            $this->dataConstructor = function ($class) {
-                return new $class();
-            };
         }
         }
 
 
         parent::__construct($name, $options);
         parent::__construct($name, $options);
@@ -356,9 +352,14 @@ class Form extends Field implements \IteratorAggregate, FormInterface
      */
      */
     public function setData($data)
     public function setData($data)
     {
     {
-        if (empty($data) && $this->dataClass) {
-            $constructor = $this->dataConstructor;
-            $data = $constructor($this->dataClass);
+        if (empty($data)) {
+            if ($this->dataConstructor) {
+                $constructor = $this->dataConstructor;
+                $data = $constructor();
+            } else if ($this->dataClass) {
+                $class = $this->dataClass;
+                $data = new $class();
+            }
         }
         }
 
 
         parent::setData($data);
         parent::setData($data);

+ 1 - 5
tests/Symfony/Tests/Component/Form/FormTest.php

@@ -1011,13 +1011,9 @@ class FormTest extends \PHPUnit_Framework_TestCase
 
 
     public function testSetDataToNullUsesDataConstructorOption()
     public function testSetDataToNullUsesDataConstructorOption()
     {
     {
-        $test = $this;
         $author = new Author();
         $author = new Author();
         $form = new Form('author', array(
         $form = new Form('author', array(
-            'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
-            'data_constructor' => function ($class) use ($test, $author) {
-                $test->assertEquals('Symfony\Tests\Component\Form\Fixtures\Author', $class);
-
+            'data_constructor' => function () use ($author) {
                 return $author;
                 return $author;
             }
             }
         ));
         ));