diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index ebd69f550d..1cd9f7ff26 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -112,10 +112,6 @@ class Form extends Field implements \IteratorAggregate, FormInterface if (isset($options['data_constructor'])) { $this->dataConstructor = $options['data_constructor']; - } else { - $this->dataConstructor = function ($class) { - return new $class(); - }; } parent::__construct($name, $options); @@ -356,9 +352,14 @@ class Form extends Field implements \IteratorAggregate, FormInterface */ 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); diff --git a/tests/Symfony/Tests/Component/Form/FormTest.php b/tests/Symfony/Tests/Component/Form/FormTest.php index e708824d8e..da609f48b2 100644 --- a/tests/Symfony/Tests/Component/Form/FormTest.php +++ b/tests/Symfony/Tests/Component/Form/FormTest.php @@ -1011,13 +1011,9 @@ class FormTest extends \PHPUnit_Framework_TestCase public function testSetDataToNullUsesDataConstructorOption() { - $test = $this; $author = new Author(); $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; } ));