[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'])) {
|
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) {
|
if (empty($data)) {
|
||||||
$constructor = $this->dataConstructor;
|
if ($this->dataConstructor) {
|
||||||
$data = $constructor($this->dataClass);
|
$constructor = $this->dataConstructor;
|
||||||
|
$data = $constructor();
|
||||||
|
} else if ($this->dataClass) {
|
||||||
|
$class = $this->dataClass;
|
||||||
|
$data = new $class();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::setData($data);
|
parent::setData($data);
|
||||||
|
@ -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 () use ($author) {
|
||||||
'data_constructor' => function ($class) use ($test, $author) {
|
|
||||||
$test->assertEquals('Symfony\Tests\Component\Form\Fixtures\Author', $class);
|
|
||||||
|
|
||||||
return $author;
|
return $author;
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
Reference in New Issue
Block a user