From 6fd287523631a50019cc4af9e2a5c25ef21ebe35 Mon Sep 17 00:00:00 2001 From: Trent Steel Date: Mon, 13 Jan 2014 15:50:51 +1000 Subject: [PATCH 1/3] add option to ignore invalid choices. references #9738 --- .../FixCheckboxInputListener.php | 8 +++++-- .../Form/Extension/Core/Type/ChoiceType.php | 23 ++++++++++--------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php b/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php index 8e72b04137b79..fedb6b8619e6b 100644 --- a/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php +++ b/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php @@ -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) @@ -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)) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index cdaee86b25b03..a4b3dae1f2e2f 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -70,7 +70,7 @@ 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); @@ -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( From f13d3db77b9899f84c832fd5f33d43e13afa48b4 Mon Sep 17 00:00:00 2001 From: Trent Steel Date: Wed, 15 Jan 2014 08:37:02 +1000 Subject: [PATCH 2/3] replicate ignore_invalid_choices logic on non expanded fields --- .../Core/DataTransformer/ChoicesToValuesTransformer.php | 8 ++++++-- .../Component/Form/Extension/Core/Type/ChoiceType.php | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php index 4492865e5a64b..0cd0409a6670b 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/ChoicesToValuesTransformer.php @@ -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; } /** @@ -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'); } diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index a4b3dae1f2e2f..e3b0b076cd47b 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -77,7 +77,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) } } 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'])); } From c20a0185bfdfe9575b57c328944b0c3aa4459857 Mon Sep 17 00:00:00 2001 From: Trent Steel Date: Wed, 15 Jan 2014 09:10:51 +1000 Subject: [PATCH 3/3] add test for ignoring invalid choices --- .../ChoicesToValuesTransformerTest.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/ChoicesToValuesTransformerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/ChoicesToValuesTransformerTest.php index 572971938d527..36166aa54c4b8 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/ChoicesToValuesTransformerTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/ChoicesToValuesTransformerTest.php @@ -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() @@ -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)); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy