Skip to content

[Form] Forms now don't create empty objects anymore if they are empty and not required #3257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
model
* added options "adder_prefix" and "remover_prefix" to collection and choice
type
* forms now don't create an empty object anymore if they are completely
empty and not required. The empty value for such forms is null.

### HttpFoundation

Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/FieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ public function getDefaultOptions(array $options)
}

if ($class) {
$defaultOptions['empty_data'] = function () use ($class) {
$defaultOptions['empty_data'] = function (FormInterface $form) use ($class) {
if ($form->isEmpty() && !$form->isRequired()) {
return null;
}

return new $class();
};
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ public function bind($clientData)
}

// Merge form data from children into existing client data
if (count($this->children) > 0 && $this->dataMapper) {
if (count($this->children) > 0 && $this->dataMapper && null !== $clientData) {
$this->dataMapper->mapFormsToData($this->children, $clientData);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,18 +178,52 @@ public function testBindWithEmptyDataCreatesObjectIfClassAvailable()
{
$form = $this->factory->create('form', null, array(
'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
'required' => false,
));
$form->add($this->factory->createNamed('field', 'firstName'));
$form->add($this->factory->createNamed('field', 'lastName'));

$form->setData(null);
$form->bind(array('firstName' => 'Bernhard'));
// partially empty, still an object is created
$form->bind(array('firstName' => 'Bernhard', 'lastName' => ''));

$author = new Author();
$author->firstName = 'Bernhard';
$author->setLastName('');

$this->assertEquals($author, $form->getData());
}

public function testBindEmptyWithEmptyDataCreatesNoObjectIfNotRequired()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo? two emptys

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No typo. It's bound empty (without data) while the form has no (empty) data. It's slightly confusing, I agree.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. The other test names also confused me. I think it would be more clear with on preposition.
So testBindEmptyDataOnEmptyDataCreatesNoObjectIfNotRequired and testBindOnEmptyDataCreatesObjectIfClassAvailable.

{
$form = $this->factory->create('form', null, array(
'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
'required' => false,
));
$form->add($this->factory->createNamed('field', 'firstName'));
$form->add($this->factory->createNamed('field', 'lastName'));

$form->setData(null);
$form->bind(array('firstName' => '', 'lastName' => ''));

$this->assertNull($form->getData());
}

public function testBindEmptyWithEmptyDataCreatesObjectIfRequired()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

{
$form = $this->factory->create('form', null, array(
'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
'required' => true,
));
$form->add($this->factory->createNamed('field', 'firstName'));
$form->add($this->factory->createNamed('field', 'lastName'));

$form->setData(null);
$form->bind(array('firstName' => '', 'lastName' => ''));

$this->assertEquals(new Author(), $form->getData());
}

/*
* We need something to write the field values into
*/
Expand Down