Skip to content

Commit

Permalink
[Form] Fixed: "data_constructor" option is used even if "data_class" …
Browse files Browse the repository at this point in the history
…option is not set
  • Loading branch information
Bernhard Schussek authored and fabpot committed Feb 8, 2011
1 parent 5b95805 commit f51dafc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
15 changes: 8 additions & 7 deletions src/Symfony/Component/Form/Form.php
Expand Up @@ -112,10 +112,6 @@ public function __construct($name = null, array $options = array())

if (isset($options['data_constructor'])) {
$this->dataConstructor = $options['data_constructor'];
} else {
$this->dataConstructor = function ($class) {
return new $class();
};
}

parent::__construct($name, $options);
Expand Down Expand Up @@ -356,9 +352,14 @@ protected function getFieldsByVisibility($hidden, $recursive)
*/
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);
Expand Down
6 changes: 1 addition & 5 deletions tests/Symfony/Tests/Component/Form/FormTest.php
Expand Up @@ -1011,13 +1011,9 @@ public function testSetDataToNullCreatesObjectIfClassAvailable()

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;
}
));
Expand Down

0 comments on commit f51dafc

Please sign in to comment.