[Form] Fixed: "data_constructor" option is used even if "data_class" option is not set
This commit is contained in:
parent
5b95805340
commit
f51dafca3f
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
));
|
||||
|
Reference in New Issue
Block a user