Skip to content

Commit 4dab38b

Browse files
committed
Merge branch '2.7' into 2.8
* 2.7: revert typo fix [Form] Fix ChoiceType to ensure submitted data is not nested unnecessarily Test inline styles with non-decorated formatter Fix RuntimeException when an Emacs buffer is modified [Yaml] add tests for specific mapping keys
2 parents 2997e0c + ea7025b commit 4dab38b

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

Extension/Core/Type/ChoiceType.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,22 @@ public function buildForm(FormBuilderInterface $builder, array $options)
160160
// transformation is merged back into the original collection
161161
$builder->addEventSubscriber(new MergeCollectionListener(true, true));
162162
}
163+
164+
// To avoid issues when the submitted choices are arrays (i.e. array to string conversions),
165+
// we have to ensure that all elements of the submitted choice data are strings or null.
166+
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
167+
$data = $event->getData();
168+
169+
if (!is_array($data)) {
170+
return;
171+
}
172+
173+
foreach ($data as $v) {
174+
if (null !== $v && !is_string($v)) {
175+
throw new TransformationFailedException('All choices submitted must be NULL or strings.');
176+
}
177+
}
178+
}, 256);
163179
}
164180

165181
/**
@@ -523,7 +539,7 @@ private function createChoiceListView(ChoiceListInterface $choiceList, array $op
523539
*
524540
* @param array|\Traversable $choices The choice labels indexed by choices
525541
* @param object $choiceLabels The object that receives the choice labels
526-
* indexed by generated keys.
542+
* indexed by generated keys
527543
* @param int $nextKey The next generated key
528544
*
529545
* @return array The choices in a normalized array with labels replaced by generated keys

Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
1515
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
1616
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
17+
use Symfony\Component\Form\Test\TypeTestCase;
1718

18-
class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
19+
class ChoiceTypeTest extends TypeTestCase
1920
{
2021
private $choices = array(
2122
'Bernhard' => 'a',
@@ -2298,4 +2299,30 @@ public function testCustomChoiceTypeDoesNotInheritChoiceLabels()
22982299
// In this case the 'choice_label' closure returns null and not the closure from the first choice type.
22992300
$this->assertNull($form->get('subChoice')->getConfig()->getOption('choice_label'));
23002301
}
2302+
2303+
/**
2304+
* @dataProvider invalidNestedValueTestMatrix
2305+
*/
2306+
public function testSubmitInvalidNestedValue($multiple, $expanded, $submissionData)
2307+
{
2308+
$form = $this->factory->create('choice', null, array(
2309+
'choices' => $this->choices,
2310+
'multiple' => $multiple,
2311+
'expanded' => $expanded,
2312+
));
2313+
2314+
$form->submit($submissionData);
2315+
$this->assertFalse($form->isSynchronized());
2316+
$this->assertEquals('All choices submitted must be NULL or strings.', $form->getTransformationFailure()->getMessage());
2317+
}
2318+
2319+
public function invalidNestedValueTestMatrix()
2320+
{
2321+
return array(
2322+
'non-multiple, non-expanded' => array(false, false, array(array())),
2323+
'non-multiple, expanded' => array(false, true, array(array())),
2324+
'multiple, non-expanded' => array(true, false, array(array())),
2325+
'multiple, expanded' => array(true, true, array(array())),
2326+
);
2327+
}
23012328
}

0 commit comments

Comments
 (0)