Skip to content

Commit 679dad2

Browse files
committed
Merge branch '2.8' into 3.2
* 2.8: fixed tests 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 c2ce40b + 8be16f2 commit 679dad2

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

Extension/Core/Type/ChoiceType.php

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

161177
/**

Tests/Extension/Core/Type/ChoiceTypeTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
1515
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
16+
use Symfony\Component\Form\Extension\Core\ChoiceList\ObjectChoiceList;
17+
use Symfony\Component\Form\Test\TypeTestCase;
1618

17-
class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
19+
class ChoiceTypeTest extends TypeTestCase
1820
{
1921
private $choices = array(
2022
'Bernhard' => 'a',
@@ -1743,4 +1745,31 @@ public function testCustomChoiceTypeDoesNotInheritChoiceLabels()
17431745
// In this case the 'choice_label' closure returns null and not the closure from the first choice type.
17441746
$this->assertNull($form->get('subChoice')->getConfig()->getOption('choice_label'));
17451747
}
1748+
1749+
/**
1750+
* @dataProvider invalidNestedValueTestMatrix
1751+
*/
1752+
public function testSubmitInvalidNestedValue($multiple, $expanded, $submissionData)
1753+
{
1754+
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array(
1755+
'choices' => $this->choices,
1756+
'multiple' => $multiple,
1757+
'expanded' => $expanded,
1758+
'choices_as_values' => true,
1759+
));
1760+
1761+
$form->submit($submissionData);
1762+
$this->assertFalse($form->isSynchronized());
1763+
$this->assertEquals('All choices submitted must be NULL or strings.', $form->getTransformationFailure()->getMessage());
1764+
}
1765+
1766+
public function invalidNestedValueTestMatrix()
1767+
{
1768+
return array(
1769+
'non-multiple, non-expanded' => array(false, false, array(array())),
1770+
'non-multiple, expanded' => array(false, true, array(array())),
1771+
'multiple, non-expanded' => array(true, false, array(array())),
1772+
'multiple, expanded' => array(true, true, array(array())),
1773+
);
1774+
}
17461775
}

0 commit comments

Comments
 (0)