From 5b95805340822da53aacc59229b747d590b26037 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 7 Feb 2011 18:52:37 +0100 Subject: [PATCH] [Form] Added option "data_constructor" to Form. When a Form is initialized with NULL, an object is automatically created using this constructor. If no constructor is given but the option "data_class" is set, an object of that class is created with the default constructor instead. --- src/Symfony/Component/Form/Form.php | 20 +++++++++++ .../Symfony/Tests/Component/Form/FormTest.php | 35 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index fd1f1d11a3..ebd69f550d 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -58,6 +58,12 @@ class Form extends Field implements \IteratorAggregate, FormInterface */ protected $dataClass; + /** + * Stores the constructor closure for creating new domain object instances + * @var \Closure + */ + protected $dataConstructor; + /** * The context used when creating the form * @var FormContext @@ -86,6 +92,7 @@ class Form extends Field implements \IteratorAggregate, FormInterface public function __construct($name = null, array $options = array()) { $this->addOption('data_class'); + $this->addOption('data_constructor'); $this->addOption('csrf_field_name', '_token'); $this->addOption('csrf_provider'); $this->addOption('field_factory'); @@ -103,6 +110,14 @@ class Form extends Field implements \IteratorAggregate, FormInterface $this->dataClass = $options['data_class']; } + if (isset($options['data_constructor'])) { + $this->dataConstructor = $options['data_constructor']; + } else { + $this->dataConstructor = function ($class) { + return new $class(); + }; + } + parent::__construct($name, $options); // Enable CSRF protection @@ -341,6 +356,11 @@ class Form extends Field implements \IteratorAggregate, FormInterface */ public function setData($data) { + if (empty($data) && $this->dataClass) { + $constructor = $this->dataConstructor; + $data = $constructor($this->dataClass); + } + parent::setData($data); // get transformed data and pass its values to child fields diff --git a/tests/Symfony/Tests/Component/Form/FormTest.php b/tests/Symfony/Tests/Component/Form/FormTest.php index 711274a14d..e708824d8e 100644 --- a/tests/Symfony/Tests/Component/Form/FormTest.php +++ b/tests/Symfony/Tests/Component/Form/FormTest.php @@ -991,6 +991,41 @@ class FormTest extends \PHPUnit_Framework_TestCase $form->setData(new Author()); } + public function testSetDataToNull() + { + $form = new Form('author'); + $form->setData(null); + + $this->assertNull($form->getData()); + } + + public function testSetDataToNullCreatesObjectIfClassAvailable() + { + $form = new Form('author', array( + 'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author', + )); + $form->setData(null); + + $this->assertEquals(new Author(), $form->getData()); + } + + 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); + + return $author; + } + )); + $form->setData(null); + + $this->assertSame($author, $form->getData()); + } + public function testSubmitUpdatesTransformedDataFromAllFields() { $originalAuthor = new Author();