Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7997f24

Browse files
committed
[Form] Fixed regression: Empty values were not accepted anymore for collapsed, optional choice fields
1 parent b26077b commit 7997f24

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoiceToValueTransformer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public function reverseTransform($value)
4646
$choices = $this->choiceList->getChoicesForValues(array((string) $value));
4747

4848
if (1 !== count($choices)) {
49+
if (null === $value || '' === $value) {
50+
return;
51+
}
52+
4953
throw new TransformationFailedException(sprintf('The choice "%s" does not exist or is not unique', $value));
5054
}
5155

src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ public function testSubmitSingleNonExpanded()
240240

241241
$this->assertEquals('b', $form->getData());
242242
$this->assertEquals('b', $form->getViewData());
243+
$this->assertTrue($form->isSynchronized());
243244
}
244245

245246
public function testSubmitSingleNonExpandedInvalidChoice()
@@ -269,6 +270,7 @@ public function testSubmitSingleNonExpandedNull()
269270

270271
$this->assertNull($form->getData());
271272
$this->assertSame('', $form->getViewData());
273+
$this->assertTrue($form->isSynchronized());
272274
}
273275

274276
// In edge cases (for example, when choices are loaded dynamically by a
@@ -286,6 +288,7 @@ public function testSubmitSingleNonExpandedNullNoChoices()
286288

287289
$this->assertNull($form->getData());
288290
$this->assertSame('', $form->getViewData());
291+
$this->assertTrue($form->isSynchronized());
289292
}
290293

291294
public function testSubmitSingleNonExpandedEmpty()
@@ -300,6 +303,27 @@ public function testSubmitSingleNonExpandedEmpty()
300303

301304
$this->assertNull($form->getData());
302305
$this->assertSame('', $form->getViewData());
306+
$this->assertTrue($form->isSynchronized());
307+
}
308+
309+
public function testSubmitSingleNonExpandedEmptyExplicitEmptyChoice()
310+
{
311+
$form = $this->factory->create('choice', null, array(
312+
'multiple' => false,
313+
'expanded' => false,
314+
'choices' => array(
315+
'EMPTY_CHOICE' => 'Empty',
316+
),
317+
'choice_value' => function () {
318+
return '';
319+
},
320+
));
321+
322+
$form->submit('');
323+
324+
$this->assertSame('EMPTY_CHOICE', $form->getData());
325+
$this->assertSame('', $form->getViewData());
326+
$this->assertTrue($form->isSynchronized());
303327
}
304328

305329
// In edge cases (for example, when choices are loaded dynamically by a
@@ -317,6 +341,7 @@ public function testSubmitSingleNonExpandedEmptyNoChoices()
317341

318342
$this->assertNull($form->getData());
319343
$this->assertSame('', $form->getViewData());
344+
$this->assertTrue($form->isSynchronized());
320345
}
321346

322347
public function testSubmitSingleNonExpandedFalse()
@@ -331,6 +356,7 @@ public function testSubmitSingleNonExpandedFalse()
331356

332357
$this->assertNull($form->getData());
333358
$this->assertSame('', $form->getViewData());
359+
$this->assertTrue($form->isSynchronized());
334360
}
335361

336362
// In edge cases (for example, when choices are loaded dynamically by a
@@ -348,6 +374,7 @@ public function testSubmitSingleNonExpandedFalseNoChoices()
348374

349375
$this->assertNull($form->getData());
350376
$this->assertSame('', $form->getViewData());
377+
$this->assertTrue($form->isSynchronized());
351378
}
352379

353380
public function testSubmitSingleNonExpandedObjectChoices()
@@ -366,6 +393,7 @@ public function testSubmitSingleNonExpandedObjectChoices()
366393

367394
$this->assertEquals($this->objectChoices[1], $form->getData());
368395
$this->assertEquals('2', $form->getViewData());
396+
$this->assertTrue($form->isSynchronized());
369397
}
370398

371399
/**
@@ -392,6 +420,7 @@ public function testLegacySubmitSingleNonExpandedObjectChoices()
392420

393421
$this->assertEquals($this->objectChoices[1], $form->getData());
394422
$this->assertEquals('2', $form->getViewData());
423+
$this->assertTrue($form->isSynchronized());
395424
}
396425

397426
public function testSubmitMultipleNonExpanded()
@@ -406,6 +435,7 @@ public function testSubmitMultipleNonExpanded()
406435

407436
$this->assertEquals(array('a', 'b'), $form->getData());
408437
$this->assertEquals(array('a', 'b'), $form->getViewData());
438+
$this->assertTrue($form->isSynchronized());
409439
}
410440

411441
public function testSubmitMultipleNonExpandedEmpty()
@@ -420,6 +450,7 @@ public function testSubmitMultipleNonExpandedEmpty()
420450

421451
$this->assertSame(array(), $form->getData());
422452
$this->assertSame(array(), $form->getViewData());
453+
$this->assertTrue($form->isSynchronized());
423454
}
424455

425456
// In edge cases (for example, when choices are loaded dynamically by a
@@ -437,6 +468,7 @@ public function testSubmitMultipleNonExpandedEmptyNoChoices()
437468

438469
$this->assertSame(array(), $form->getData());
439470
$this->assertSame(array(), $form->getViewData());
471+
$this->assertTrue($form->isSynchronized());
440472
}
441473

442474
public function testSubmitMultipleNonExpandedInvalidScalarChoice()
@@ -484,6 +516,7 @@ public function testSubmitMultipleNonExpandedObjectChoices()
484516

485517
$this->assertEquals(array($this->objectChoices[1], $this->objectChoices[2]), $form->getData());
486518
$this->assertEquals(array('2', '3'), $form->getViewData());
519+
$this->assertTrue($form->isSynchronized());
487520
}
488521

489522
/**
@@ -509,6 +542,7 @@ public function testLegacySubmitMultipleNonExpandedObjectChoices()
509542

510543
$this->assertEquals(array($this->objectChoices[1], $this->objectChoices[2]), $form->getData());
511544
$this->assertEquals(array('2', '3'), $form->getViewData());
545+
$this->assertTrue($form->isSynchronized());
512546
}
513547

514548
public function testSubmitSingleExpandedRequired()
@@ -933,6 +967,8 @@ public function testSubmitSingleExpandedWithEmptyChild()
933967
$form->submit('');
934968

935969
$this->assertNull($form->getData());
970+
$this->assertTrue($form->isSynchronized());
971+
936972
$this->assertTrue($form[0]->getData());
937973
$this->assertFalse($form[1]->getData());
938974
$this->assertSame('', $form[0]->getViewData());
@@ -953,6 +989,8 @@ public function testSubmitSingleExpandedObjectChoices()
953989
$form->submit('2');
954990

955991
$this->assertSame($this->objectChoices[1], $form->getData());
992+
$this->assertTrue($form->isSynchronized());
993+
956994
$this->assertFalse($form[0]->getData());
957995
$this->assertTrue($form[1]->getData());
958996
$this->assertFalse($form[2]->getData());
@@ -987,6 +1025,8 @@ public function testLegacySubmitSingleExpandedObjectChoices()
9871025
$form->submit('2');
9881026

9891027
$this->assertSame($this->objectChoices[1], $form->getData());
1028+
$this->assertTrue($form->isSynchronized());
1029+
9901030
$this->assertFalse($form[0]->getData());
9911031
$this->assertTrue($form[1]->getData());
9921032
$this->assertFalse($form[2]->getData());
@@ -1010,6 +1050,8 @@ public function testSubmitSingleExpandedNumericChoices()
10101050
$form->submit('1');
10111051

10121052
$this->assertSame(1, $form->getData());
1053+
$this->assertTrue($form->isSynchronized());
1054+
10131055
$this->assertFalse($form[0]->getData());
10141056
$this->assertTrue($form[1]->getData());
10151057
$this->assertFalse($form[2]->getData());
@@ -1114,6 +1156,8 @@ public function testSubmitMultipleExpandedEmpty()
11141156
$form->submit(array());
11151157

11161158
$this->assertSame(array(), $form->getData());
1159+
$this->assertTrue($form->isSynchronized());
1160+
11171161
$this->assertFalse($form[0]->getData());
11181162
$this->assertFalse($form[1]->getData());
11191163
$this->assertFalse($form[2]->getData());
@@ -1140,6 +1184,7 @@ public function testSubmitMultipleExpandedEmptyNoChoices()
11401184
$form->submit(array());
11411185

11421186
$this->assertSame(array(), $form->getData());
1187+
$this->assertTrue($form->isSynchronized());
11431188
}
11441189

11451190
public function testSubmitMultipleExpandedWithEmptyChild()
@@ -1157,6 +1202,8 @@ public function testSubmitMultipleExpandedWithEmptyChild()
11571202
$form->submit(array('', '2'));
11581203

11591204
$this->assertSame(array('', 2), $form->getData());
1205+
$this->assertTrue($form->isSynchronized());
1206+
11601207
$this->assertTrue($form[0]->getData());
11611208
$this->assertFalse($form[1]->getData());
11621209
$this->assertTrue($form[2]->getData());
@@ -1179,6 +1226,8 @@ public function testSubmitMultipleExpandedObjectChoices()
11791226
$form->submit(array('1', '2'));
11801227

11811228
$this->assertSame(array($this->objectChoices[0], $this->objectChoices[1]), $form->getData());
1229+
$this->assertTrue($form->isSynchronized());
1230+
11821231
$this->assertTrue($form[0]->getData());
11831232
$this->assertTrue($form[1]->getData());
11841233
$this->assertFalse($form[2]->getData());
@@ -1213,6 +1262,8 @@ public function testLegacySubmitMultipleExpandedObjectChoices()
12131262
$form->submit(array('1', '2'));
12141263

12151264
$this->assertSame(array($this->objectChoices[0], $this->objectChoices[1]), $form->getData());
1265+
$this->assertTrue($form->isSynchronized());
1266+
12161267
$this->assertTrue($form[0]->getData());
12171268
$this->assertTrue($form[1]->getData());
12181269
$this->assertFalse($form[2]->getData());
@@ -1236,6 +1287,8 @@ public function testSubmitMultipleExpandedNumericChoices()
12361287
$form->submit(array('1', '2'));
12371288

12381289
$this->assertSame(array(1, 2), $form->getData());
1290+
$this->assertTrue($form->isSynchronized());
1291+
12391292
$this->assertFalse($form[0]->getData());
12401293
$this->assertTrue($form[1]->getData());
12411294
$this->assertTrue($form[2]->getData());
@@ -1264,6 +1317,7 @@ public function testSetDataSingleNonExpandedAcceptsBoolean()
12641317

12651318
$this->assertFalse($form->getData());
12661319
$this->assertEquals('0', $form->getViewData());
1320+
$this->assertTrue($form->isSynchronized());
12671321
}
12681322

12691323
public function testSetDataMultipleNonExpandedAcceptsBoolean()
@@ -1278,6 +1332,7 @@ public function testSetDataMultipleNonExpandedAcceptsBoolean()
12781332

12791333
$this->assertEquals(array(false, true), $form->getData());
12801334
$this->assertEquals(array('0', '1'), $form->getViewData());
1335+
$this->assertTrue($form->isSynchronized());
12811336
}
12821337

12831338
public function testPassRequiredToView()
@@ -1344,7 +1399,7 @@ public function testChoiceTranslationDomainWithTrueValueToView()
13441399
$this->assertNull($view->vars['choice_translation_domain']);
13451400
}
13461401

1347-
public function testDefaulChoiceTranslationDomainIsSameAsTranslationDomainToView()
1402+
public function testDefaultChoiceTranslationDomainIsSameAsTranslationDomainToView()
13481403
{
13491404
$form = $this->factory->create('choice', null, array(
13501405
'choices' => $this->choices,

0 commit comments

Comments
 (0)