Skip to content

Commit

Permalink
[Form] Fixed trimming choice values
Browse files Browse the repository at this point in the history
  • Loading branch information
HeahDude committed Apr 15, 2018
1 parent e23b559 commit 87ee384
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ public function configureOptions(OptionsResolver $resolver)
// See https://github.com/symfony/symfony/pull/5582
'data_class' => null,
'choice_translation_domain' => true,
'trim' => false,
));

$resolver->setNormalizer('choices', $choicesNormalizer);
Expand Down
55 changes: 55 additions & 0 deletions Tests/Extension/Core/Type/ChoiceTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2421,4 +2421,59 @@ public function invalidNestedValueTestMatrix()
'multiple, expanded' => array(true, true, array(array())),
);
}

/**
* @dataProvider provideTrimCases
*/
public function testTrimIsDisabled($multiple, $expanded)
{
$form = $this->factory->create(static::TESTED_TYPE, null, array(
'multiple' => $multiple,
'expanded' => $expanded,
'choices' => array(
'a' => '1',
),
'choices_as_values' => true,
));

$submittedData = ' 1';

$form->submit($multiple ? (array) $submittedData : $submittedData);

// When the choice does not exist the transformation fails
$this->assertFalse($form->isSynchronized());
$this->assertNull($form->getData());
}

/**
* @dataProvider provideTrimCases
*/
public function testSubmitValueWithWhiteSpace($multiple, $expanded)
{
$valueWhitWhiteSpace = '1 ';

$form = $this->factory->create(static::TESTED_TYPE, null, array(
'multiple' => $multiple,
'expanded' => $expanded,
'choices' => array(
'a' => $valueWhitWhiteSpace,
),
'choices_as_values' => true,
));

$form->submit($multiple ? (array) $valueWhitWhiteSpace : $valueWhitWhiteSpace);

$this->assertTrue($form->isSynchronized());
$this->assertSame($multiple ? (array) $valueWhitWhiteSpace : $valueWhitWhiteSpace, $form->getData());
}

public function provideTrimCases()
{
return array(
'Simple' => array(false, false),
'Multiple' => array(true, false),
'Simple expanded' => array(false, true),
'Multiple expanded' => array(true, true),
);
}
}

0 comments on commit 87ee384

Please sign in to comment.