[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.
This commit is contained in:
parent
09a50c3c55
commit
5b95805340
@ -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
|
||||
|
@ -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();
|
||||
|
Reference in New Issue
Block a user