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

Skip to content

[Form] Add option to ignore invalid choices #10008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ class ChoicesToValuesTransformer implements DataTransformerInterface
{
private $choiceList;

private $ignoreInvalidChoices;

/**
* Constructor.
*
* @param ChoiceListInterface $choiceList
* @param bool $ignoreInvalidChoices
*/
public function __construct(ChoiceListInterface $choiceList)
public function __construct(ChoiceListInterface $choiceList, $ignoreInvalidChoices)
{
$this->choiceList = $choiceList;
$this->ignoreInvalidChoices = $ignoreInvalidChoices;
}

/**
Expand Down Expand Up @@ -74,7 +78,7 @@ public function reverseTransform($array)

$choices = $this->choiceList->getChoicesForValues($array);

if (count($choices) !== count($array)) {
if (!$this->ignoreInvalidChoices && count($choices) !== count($array)) {
throw new TransformationFailedException('Could not find all matching choices for the given values');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ class FixCheckboxInputListener implements EventSubscriberInterface
{
private $choiceList;

private $ignoreInvalidChoices;

/**
* Constructor.
*
* @param ChoiceListInterface $choiceList
* @param bool $ignoreInvalidChoices
*/
public function __construct(ChoiceListInterface $choiceList)
public function __construct(ChoiceListInterface $choiceList, $ignoreInvalidChoices)
{
$this->choiceList = $choiceList;
$this->ignoreInvalidChoices = $ignoreInvalidChoices;
}

public function preSubmit(FormEvent $event)
Expand Down Expand Up @@ -62,7 +66,7 @@ public function preSubmit(FormEvent $event)
}
}

if (count($submittedValues) > 0) {
if (!$this->ignoreInvalidChoices && count($submittedValues) > 0) {
throw new TransformationFailedException(sprintf(
'The following choices were not found: "%s"',
implode('", "', array_keys($submittedValues))
Expand Down
25 changes: 13 additions & 12 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ public function buildForm(FormBuilderInterface $builder, array $options)

if ($options['multiple']) {
$builder->addViewTransformer(new ChoicesToBooleanArrayTransformer($options['choice_list']));
$builder->addEventSubscriber(new FixCheckboxInputListener($options['choice_list']), 10);
$builder->addEventSubscriber(new FixCheckboxInputListener($options['choice_list'], $options['ignore_invalid_choices']), 10);
} else {
$builder->addViewTransformer(new ChoiceToBooleanArrayTransformer($options['choice_list'], $builder->has('placeholder')));
$builder->addEventSubscriber(new FixRadioInputListener($options['choice_list'], $builder->has('placeholder')), 10);
}
} else {
if ($options['multiple']) {
$builder->addViewTransformer(new ChoicesToValuesTransformer($options['choice_list']));
$builder->addViewTransformer(new ChoicesToValuesTransformer($options['choice_list'], $options['ignore_invalid_choices']));
} else {
$builder->addViewTransformer(new ChoiceToValueTransformer($options['choice_list']));
}
Expand Down Expand Up @@ -208,19 +208,20 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
};

$resolver->setDefaults(array(
'multiple' => false,
'expanded' => false,
'choice_list' => $choiceList,
'choices' => array(),
'preferred_choices' => array(),
'empty_data' => $emptyData,
'empty_value' => $emptyValue,
'error_bubbling' => false,
'compound' => $compound,
'multiple' => false,
'expanded' => false,
'choice_list' => $choiceList,
'choices' => array(),
'preferred_choices' => array(),
'ignore_invalid_choices' => false,
'empty_data' => $emptyData,
'empty_value' => $emptyValue,
'error_bubbling' => false,
'compound' => $compound,
// The view data is always a string, even if the "data" option
// is manually set to an object.
// See https://github.com/symfony/symfony/pull/5582
'data_class' => null,
'data_class' => null,
));

$resolver->setNormalizers(array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ class ChoicesToValuesTransformerTest extends \PHPUnit_Framework_TestCase
{
protected $transformer;

protected $transformer2;

protected function setUp()
{
$list = new SimpleChoiceList(array(0 => 'A', 1 => 'B', 2 => 'C'));
$this->transformer = new ChoicesToValuesTransformer($list);
$this->transformer = new ChoicesToValuesTransformer($list, false);
$this->transformer2 = new ChoicesToValuesTransformer($list, true);
}

protected function tearDown()
{
$this->transformer = null;
$this->transformer2 = null;
}

public function testTransform()
Expand Down Expand Up @@ -73,4 +77,13 @@ public function testReverseTransformExpectsArray()
{
$this->transformer->reverseTransform('foobar');
}

public function testIgnoreInvalidChoicesTransform()
{
// Value strategy in SimpleChoiceList is to copy and convert to string
$in = array(2, 3, 4, 5);
$out = array('2');

$this->assertSame($out, $this->transformer2->transform($in));
}
}