Skip to content

Commit 00cdf5e

Browse files
committed
[Form] Fixed trimming choice values
1 parent 1067468 commit 00cdf5e

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ public function configureOptions(OptionsResolver $resolver)
413413
// See https://github.com/symfony/symfony/pull/5582
414414
'data_class' => null,
415415
'choice_translation_domain' => true,
416+
'trim' => false,
416417
));
417418

418419
$resolver->setNormalizer('choices', $choicesNormalizer);

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2421,4 +2421,59 @@ public function invalidNestedValueTestMatrix()
24212421
'multiple, expanded' => array(true, true, array(array())),
24222422
);
24232423
}
2424+
2425+
/**
2426+
* @dataProvider provideTrimCases
2427+
*/
2428+
public function testTrimIsDisabled($multiple, $expanded)
2429+
{
2430+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
2431+
'multiple' => $multiple,
2432+
'expanded' => $expanded,
2433+
'choices' => array(
2434+
'a' => '1',
2435+
),
2436+
'choices_as_values' => true,
2437+
));
2438+
2439+
$submittedData = ' 1';
2440+
2441+
$form->submit($multiple ? (array) $submittedData : $submittedData);
2442+
2443+
// When the choice does not exist the transformation fails
2444+
$this->assertFalse($form->isSynchronized());
2445+
$this->assertNull($form->getData());
2446+
}
2447+
2448+
/**
2449+
* @dataProvider provideTrimCases
2450+
*/
2451+
public function testSubmitValueWithWhiteSpace($multiple, $expanded)
2452+
{
2453+
$valueWhitWhiteSpace = '1 ';
2454+
2455+
$form = $this->factory->create(static::TESTED_TYPE, null, array(
2456+
'multiple' => $multiple,
2457+
'expanded' => $expanded,
2458+
'choices' => array(
2459+
'a' => $valueWhitWhiteSpace,
2460+
),
2461+
'choices_as_values' => true,
2462+
));
2463+
2464+
$form->submit($multiple ? (array) $valueWhitWhiteSpace : $valueWhitWhiteSpace);
2465+
2466+
$this->assertTrue($form->isSynchronized());
2467+
$this->assertSame($multiple ? (array) $valueWhitWhiteSpace : $valueWhitWhiteSpace, $form->getData());
2468+
}
2469+
2470+
public function provideTrimCases()
2471+
{
2472+
return array(
2473+
'Simple' => array(false, false),
2474+
'Multiple' => array(true, false),
2475+
'Simple expanded' => array(false, true),
2476+
'Multiple expanded' => array(true, true),
2477+
);
2478+
}
24242479
}

0 commit comments

Comments
 (0)