Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdf2ad15

Browse files
committed
merged branch bschussek/issue2861 (PR#3257)
Commits-------bd461e2 [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.Discussion----------[Form] Forms now don't create empty objects anymore if they are empty and not requiredBug fix: yesFeature addition: noBackwards compatibility break: noSymfony2 tests pass: yesFixes the following tickets:#2861Todo: -![Travis Build Status](https://secure.travis-ci.org/bschussek/symfony.png?branch=issue2861)If a form (or a nested form) is left completely empty upon submission and is not required, no new data object will be generated anymore. Instead, the form returns null as data.
2 parents0914a38 +bd461e2 commitdf2ad15

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+
returnnull;
162+
}
163+
160164
returnnew$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 =newAuthor();
188191
$author->firstName ='Bernhard';
192+
$author->setLastName('');
189193

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

197+
publicfunctiontestBindEmptyWithEmptyDataCreatesNoObjectIfNotRequired()
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+
publicfunctiontestBindEmptyWithEmptyDataCreatesObjectIfRequired()
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(newAuthor(),$form->getData());
225+
}
226+
193227
/*
194228
* We need something to write the field values into
195229
*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp