8000 [Form] Forms now don't create empty objects anymore if they are compl… · symfony/symfony@bd461e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd461e2

Browse files
committed
[Form] Forms now don't create empty objects anymore if they are completely empty and not required. The empty data for these forms is null.
1 parent 0914a38 commit bd461e2

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

CHANGELOG-2.1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
200200
model
201201
* added options "adder_prefix" and "remover_prefix" to collection and choice
202202
type
203+
* forms now don't create an empty object anymore if they are completely
204+
empty and not required. The empty value for such forms is null.
203205

204206
### HttpFoundation
205207

src/Symfony/Component/Form/Extension/Core/Type/FieldType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ public function getDefaultOptions(array $options)
156156
}
157157

158158
if ($class) {
159-
$defaultOptions['empty_data'] = function () use ($class) {
159+
$defaultOptions['empty_data'] = function (FormInterface $form) use ($class) {
160+
if ($form->isEmpty() && !$form->isRequired()) {
161+
return null;
162+
}
163+
160164
return new $class();
161165
};
162166
} else {

src/Symfony/Component/Form/Form.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ public function bind($clientData)
517517
}
518518

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

tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,52 @@ public function testBindWithEmptyDataCreatesObjectIfClassAvailable()
178178
{
179179
$form = $this->factory->create('form', null, array(
180180
'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
181+
'required' => false,
181182
));
182183
$form->add($this->factory->createNamed('field', 'firstName'));
184+
$form->add($this->factory->createNamed('field', 'lastName'));
183185

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

187190
$author = new Author();
188191
$author->firstName = 'Bernhard';
192+
$author->setLastName('');
189193

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

197+
public function testBindEmptyWithEmptyDataCreatesNoObjectIfNotRequired()
198+
{
199+
$form = $this->factory->create('form', null, array(
200+
'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
201+
'required' => false,
202+
));
203+
$form->add($this->factory->createNamed('field', 'firstName'));
204+
$form->add($this->factory->createNamed('field', 'lastName'));
205+
206+
$form->setData(null);
207+
$form->bind(array('firstName' => '', 'lastName' => ''));
208+
209+
$this->assertNull($form->getData());
210+
}
211+
212+
public function testBindEmptyWithEmptyDataCreatesObjectIfRequired()
213+
{
214+
$form = $this->factory->create('form', null, array(
215+
'data_class' => 'Symfony\Tests\Component\Form\Fixtures\Author',
216+
'required' => true,
217+
));
218+
$form->add($this->factory->createNamed('field', 'firstName'));
219+
$form->add($this->factory->createNamed('field', 'lastName'));
220+
221+
$form->setData(null);
222+
$form->bind(array('firstName' => '', 'lastName' => ''));
223+
224+
$this->assertEquals(new Author(), $form->getData());
225+
}
226+
193227
/*
194228
* We need something to write the field values into
195229
*/

0 commit comments

Comments
 (0)
0