diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index 545c1b4941af6..8e9ed8a654e9f 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -55,7 +55,9 @@ public function __construct(ChoiceListFactoryInterface $choiceListFactory = null */ public function buildForm(FormBuilderInterface $builder, array $options) { - if ('expanded' == $options['widget']) { + if (in_array($options['widget'], array('radio', 'checkbox'))) { + $options['multiple'] = 'checkbox' == $options['widget']; + $builder->setDataMapper($options['multiple'] ? new CheckboxListMapper($options['choice_list']) : new RadioListMapper($options['choice_list'])); @@ -178,7 +180,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) $view->vars = array_replace($view->vars, array( 'widget' => $options['widget'], 'multiple' => $options['multiple'], - 'expanded' => $options['expanded'], // BC + 'expanded' => in_array($options['widget'], array('radio', 'checkbox')), // BC 'preferred_choices' => $choiceListView->preferredChoices, 'choices' => $choiceListView->choices, 'separator' => '-------------------', @@ -225,7 +227,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) */ public function finishView(FormView $view, FormInterface $form, array $options) { - if ('expanded' == $options['widget']) { + if (in_array($options['widget'], array('radio', 'checkbox'))) { // Radio buttons should have the same name as the parent $childName = $view->vars['full_name']; @@ -248,11 +250,15 @@ public function configureOptions(OptionsResolver $resolver) $choiceListFactory = $this->choiceListFactory; $widget = function (Options $options) { - return $options['expanded'] ? 'expanded' : 'select'; + if ($options['expanded']) { + return $options['multiple'] ? 'checkbox' : 'radio'; + } + + return 'select'; }; $emptyData = function (Options $options) { - if ($options['multiple'] || $options['expanded']) { + if ($options['multiple'] || 'checkbox' == $options['widget']) { return array(); } @@ -304,7 +310,7 @@ public function configureOptions(OptionsResolver $resolver) } elseif (false === $placeholder) { // an empty value should be added but the user decided otherwise return; - } elseif ($options['expanded'] && '' === $placeholder) { + } elseif ('radio' == $options['widget'] && '' === $placeholder) { // never use an empty label for radio buttons return 'None'; } @@ -314,7 +320,7 @@ public function configureOptions(OptionsResolver $resolver) }; $compound = function (Options $options) { - return 'expanded' == $options['widget']; + return in_array($options['widget'], array('radio', 'checkbox')); }; $choiceTranslationDomainNormalizer = function (Options $options, $choiceTranslationDomain) { @@ -368,6 +374,8 @@ public function configureOptions(OptionsResolver $resolver) $resolver->setAllowedTypes('choice_attr', array('null', 'array', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath')); $resolver->setAllowedTypes('preferred_choices', array('array', '\Traversable', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath')); $resolver->setAllowedTypes('group_by', array('null', 'array', '\Traversable', 'string', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath')); + + $resolver->setAllowedValues('widget', array('select', 'radio', 'checkbox', 'text', 'hidden')); } /** @@ -394,7 +402,7 @@ private static function flipRecursive($choices, &$output = array()) } /** - * Adds the sub fields for an expanded choice field. + * Adds the sub fields for a radio/checkbox choice field. * * @param FormBuilderInterface $builder The form builder. * @param array $choiceViews The choice view objects. diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php index 1cf4fe66b1643..07d0dafe12636 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -102,10 +102,13 @@ public function testChoiceListAndChoicesCanBeEmpty() $this->factory->create('choice'); } - public function testExpandedChoicesOptionsTurnIntoChildren() + /** + * @dataProvider expandedWidgetsProvider + */ + public function testExpandedChoicesOptionsTurnIntoChildren($widget) { $form = $this->factory->create('choice', null, array( - 'widget' => 'expanded', + 'widget' => $widget, 'choices' => $this->choices, )); @@ -115,8 +118,7 @@ public function testExpandedChoicesOptionsTurnIntoChildren() public function testPlaceholderPresentOnNonRequiredExpandedSingleChoice() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -128,8 +130,7 @@ public function testPlaceholderPresentOnNonRequiredExpandedSingleChoice() public function testPlaceholderNotPresentIfRequired() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -142,7 +143,7 @@ public function testPlaceholderNotPresentIfMultiple() { $form = $this->factory->create('choice', null, array( 'multiple' => true, - 'widget' => 'expanded', + 'widget' => 'checkbox', 'required' => false, 'choices' => $this->choices, )); @@ -154,8 +155,7 @@ public function testPlaceholderNotPresentIfMultiple() public function testPlaceholderNotPresentIfEmptyChoice() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => false, 'choices' => array( '' => 'Empty', @@ -167,10 +167,13 @@ public function testPlaceholderNotPresentIfEmptyChoice() $this->assertCount(2, $form, 'Each choice should become a new field'); } - public function testExpandedChoicesOptionsAreFlattened() + /** + * @dataProvider expandedWidgetsProvider + */ + public function testExpandedChoicesOptionsAreFlattened($widget) { $form = $this->factory->create('choice', null, array( - 'widget' => 'expanded', + 'widget' => $widget, 'choices' => $this->groupedChoices, )); @@ -216,7 +219,7 @@ public function testExpandedCheckboxesAreNeverRequired() { $form = $this->factory->create('choice', null, array( 'multiple' => true, - 'widget' => 'expanded', + 'widget' => 'checkbox', 'required' => true, 'choices' => $this->choices, )); @@ -229,8 +232,7 @@ public function testExpandedCheckboxesAreNeverRequired() public function testExpandedRadiosAreRequiredIfChoiceChildIsRequired() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -243,8 +245,7 @@ public function testExpandedRadiosAreRequiredIfChoiceChildIsRequired() public function testExpandedRadiosAreNotRequiredIfChoiceChildIsNotRequired() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -574,8 +575,7 @@ public function testLegacySubmitMultipleNonExpandedObjectChoices() public function testSubmitSingleExpandedRequired() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -602,8 +602,7 @@ public function testSubmitSingleExpandedRequired() public function testSubmitSingleExpandedRequiredInvalidChoice() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -630,8 +629,7 @@ public function testSubmitSingleExpandedRequiredInvalidChoice() public function testSubmitSingleExpandedNonRequired() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -660,8 +658,7 @@ public function testSubmitSingleExpandedNonRequired() public function testSubmitSingleExpandedNonRequiredInvalidChoice() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -688,8 +685,7 @@ public function testSubmitSingleExpandedNonRequiredInvalidChoice() public function testSubmitSingleExpandedRequiredNull() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -719,8 +715,7 @@ public function testSubmitSingleExpandedRequiredNull() public function testSubmitSingleExpandedRequiredNullNoChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => true, 'choices' => array(), )); @@ -736,8 +731,7 @@ public function testSubmitSingleExpandedRequiredNullNoChoices() public function testSubmitSingleExpandedRequiredEmpty() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -767,8 +761,7 @@ public function testSubmitSingleExpandedRequiredEmpty() public function testSubmitSingleExpandedRequiredEmptyNoChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => true, 'choices' => array(), )); @@ -784,8 +777,7 @@ public function testSubmitSingleExpandedRequiredEmptyNoChoices() public function testSubmitSingleExpandedRequiredFalse() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -815,8 +807,7 @@ public function testSubmitSingleExpandedRequiredFalse() public function testSubmitSingleExpandedRequiredFalseNoChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => true, 'choices' => array(), )); @@ -832,8 +823,7 @@ public function testSubmitSingleExpandedRequiredFalseNoChoices() public function testSubmitSingleExpandedNonRequiredNull() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -865,8 +855,7 @@ public function testSubmitSingleExpandedNonRequiredNull() public function testSubmitSingleExpandedNonRequiredNullNoChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => false, 'choices' => array(), )); @@ -882,8 +871,7 @@ public function testSubmitSingleExpandedNonRequiredNullNoChoices() public function testSubmitSingleExpandedNonRequiredEmpty() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -915,8 +903,7 @@ public function testSubmitSingleExpandedNonRequiredEmpty() public function testSubmitSingleExpandedNonRequiredEmptyNoChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => false, 'choices' => array(), )); @@ -932,8 +919,7 @@ public function testSubmitSingleExpandedNonRequiredEmptyNoChoices() public function testSubmitSingleExpandedNonRequiredFalse() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -965,8 +951,7 @@ public function testSubmitSingleExpandedNonRequiredFalse() public function testSubmitSingleExpandedNonRequiredFalseNoChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'required' => false, 'choices' => array(), )); @@ -982,8 +967,7 @@ public function testSubmitSingleExpandedNonRequiredFalseNoChoices() public function testSubmitSingleExpandedWithEmptyChild() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'choices' => array( '' => 'Empty', 1 => 'Not empty', @@ -1004,8 +988,7 @@ public function testSubmitSingleExpandedWithEmptyChild() public function testSubmitSingleExpandedObjectChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'choices' => $this->objectChoices, 'choices_as_values' => true, 'choice_label' => 'name', @@ -1035,8 +1018,7 @@ public function testSubmitSingleExpandedObjectChoices() public function testLegacySubmitSingleExpandedObjectChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'choice_list' => new ObjectChoiceList( $this->objectChoices, // label path @@ -1068,8 +1050,7 @@ public function testLegacySubmitSingleExpandedObjectChoices() public function testSubmitSingleExpandedNumericChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => false, - 'widget' => 'expanded', + 'widget' => 'radio', 'choices' => $this->numericChoices, )); @@ -1094,7 +1075,7 @@ public function testSubmitMultipleExpanded() { $form = $this->factory->create('choice', null, array( 'multiple' => true, - 'widget' => 'expanded', + 'widget' => 'checkbox', 'choices' => $this->choices, )); @@ -1120,8 +1101,7 @@ public function testSubmitMultipleExpanded() public function testSubmitMultipleExpandedInvalidScalarChoice() { $form = $this->factory->create('choice', null, array( - 'multiple' => true, - 'widget' => 'expanded', + 'widget' => 'checkbox', 'choices' => $this->choices, )); @@ -1147,8 +1127,7 @@ public function testSubmitMultipleExpandedInvalidScalarChoice() public function testSubmitMultipleExpandedInvalidArrayChoice() { $form = $this->factory->create('choice', null, array( - 'multiple' => true, - 'widget' => 'expanded', + 'widget' => 'checkbox', 'choices' => $this->choices, )); @@ -1174,8 +1153,7 @@ public function testSubmitMultipleExpandedInvalidArrayChoice() public function testSubmitMultipleExpandedEmpty() { $form = $this->factory->create('choice', null, array( - 'multiple' => true, - 'widget' => 'expanded', + 'widget' => 'checkbox', 'choices' => $this->choices, )); @@ -1202,8 +1180,7 @@ public function testSubmitMultipleExpandedEmpty() public function testSubmitMultipleExpandedEmptyNoChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => true, - 'widget' => 'expanded', + 'widget' => 'checkbox', 'choices' => array(), )); @@ -1216,8 +1193,7 @@ public function testSubmitMultipleExpandedEmptyNoChoices() public function testSubmitMultipleExpandedWithEmptyChild() { $form = $this->factory->create('choice', null, array( - 'multiple' => true, - 'widget' => 'expanded', + 'widget' => 'checkbox', 'choices' => array( '' => 'Empty', 1 => 'Not Empty', @@ -1241,8 +1217,7 @@ public function testSubmitMultipleExpandedWithEmptyChild() public function testSubmitMultipleExpandedObjectChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => true, - 'widget' => 'expanded', + 'widget' => 'checkbox', 'choices' => $this->objectChoices, 'choices_as_values' => true, 'choice_label' => 'name', @@ -1272,8 +1247,7 @@ public function testSubmitMultipleExpandedObjectChoices() public function testLegacySubmitMultipleExpandedObjectChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => true, - 'widget' => 'expanded', + 'widget' => 'checkbox', 'choice_list' => new ObjectChoiceList( $this->objectChoices, // label path @@ -1305,8 +1279,7 @@ public function testLegacySubmitMultipleExpandedObjectChoices() public function testSubmitMultipleExpandedNumericChoices() { $form = $this->factory->create('choice', null, array( - 'multiple' => true, - 'widget' => 'expanded', + 'widget' => 'checkbox', 'choices' => $this->numericChoices, )); @@ -1393,7 +1366,7 @@ public function testPassMultipleToView() $this->assertTrue($view->vars['multiple']); } - public function testPassExpandedToView() + public function testPassExpandedToViewBC() { $form = $this->factory->create('choice', null, array( 'expanded' => true, @@ -1751,6 +1724,14 @@ public function testSubmitMultipleChoicesDelimiterAndTrimWithSimpleWidgets($widg $this->assertEquals('a|b', $form->getViewData()); } + public function expandedWidgetsProvider() + { + return array( + array('radio'), + array('checkbox'), + ); + } + public function simpleWidgetsProvider() { return array(