From 3d61584a57f1855ea2b4974b95cb40a1c15cd3fa Mon Sep 17 00:00:00 2001 From: Bilal Amarni Date: Sun, 21 Jun 2015 12:06:54 +0200 Subject: [PATCH 1/4] [Form] Add option widget to ChoiceType --- .../views/Form/form_div_layout.html.twig | 6 +- .../ValuesToStringTransformer.php | 89 +++++++++++++++++++ .../Form/Extension/Core/Type/ChoiceType.php | 48 ++++++++-- .../ValuesToStringTransformerTest.php | 71 +++++++++++++++ .../Extension/Core/Type/ChoiceTypeTest.php | 64 +++++++++++++ 5 files changed, 272 insertions(+), 6 deletions(-) create mode 100644 src/Symfony/Component/Form/Extension/Core/DataTransformer/ValuesToStringTransformer.php create mode 100644 src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/ValuesToStringTransformerTest.php diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig index 20c6e02c7ec01..84137c96cd5b9 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig @@ -35,7 +35,11 @@ {%- endblock textarea_widget -%} {%- block choice_widget -%} - {% if expanded %} + {% if 'hidden' == widget %} + {{- block('hidden_widget') -}} + {% elseif 'text' == widget %} + {{- block('form_widget_simple') -}} + {% elseif expanded %} {{- block('choice_widget_expanded') -}} {% else %} {{- block('choice_widget_collapsed') -}} diff --git a/src/Symfony/Component/Form/Extension/Core/DataTransformer/ValuesToStringTransformer.php b/src/Symfony/Component/Form/Extension/Core/DataTransformer/ValuesToStringTransformer.php new file mode 100644 index 0000000000000..2a82748ca4801 --- /dev/null +++ b/src/Symfony/Component/Form/Extension/Core/DataTransformer/ValuesToStringTransformer.php @@ -0,0 +1,89 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Extension\Core\DataTransformer; + +use Symfony\Component\Form\DataTransformerInterface; +use Symfony\Component\Form\Exception\TransformationFailedException; + +/** + * Converts an array of values to a string with multiple values separated by a delimiter. + * + * @author Bilal Amarni + */ +class ValuesToStringTransformer implements DataTransformerInterface +{ + /** + * @var string + */ + private $delimiter; + + /** + * @var bool + */ + private $trim; + + /** + * @param string $delimiter + * @param bool $trim + */ + public function __construct($delimiter, $trim) + { + $this->delimiter = $delimiter; + $this->trim = $trim; + } + + /** + * @param array $array + * + * @return string + * + * @throws UnexpectedTypeException if the given value is not an array + */ + public function transform($array) + { + if (null === $array) { + return ''; + } + + if (!is_array($array)) { + throw new TransformationFailedException('Expected an array'); + } + + return implode($this->delimiter, $array); + } + + /** + * @param string $string + * + * @return array + * + * @throws UnexpectedTypeException if the given value is not a string + */ + public function reverseTransform($string) + { + if (empty($string)) { + return array(); + } + + if (!is_string($string)) { + throw new TransformationFailedException('Expected a string'); + } + + $values = explode($this->delimiter, $string); + + if ($this->trim) { + $values = array_map('trim', $values); + } + + return $values; + } +} diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index 055391f533da2..a1414ca6dc3c1 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -31,6 +31,7 @@ use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener; use Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer; use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer; +use Symfony\Component\Form\Extension\Core\DataTransformer\ValuesToStringTransformer; use Symfony\Component\OptionsResolver\Options; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -60,7 +61,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) $choiceList = $this->createChoiceList($options); $builder->setAttribute('choice_list', $choiceList); - if ($options['expanded']) { + if ($options['expanded'] && !in_array($options['widget'], array('text', 'hidden'))) { $builder->setDataMapper($options['multiple'] ? new CheckboxListMapper($choiceList) : new RadioListMapper($choiceList)); @@ -146,10 +147,15 @@ public function buildForm(FormBuilderInterface $builder, array $options) }); } } elseif ($options['multiple']) { - // tag without "multiple" option + // "select", "text" or "hidden" tag without "multiple" option $builder->addViewTransformer(new ChoiceToValueTransformer($choiceList)); } @@ -179,8 +185,9 @@ public function buildView(FormView $view, FormInterface $form, array $options) : $this->createChoiceListView($choiceList, $options); $view->vars = array_replace($view->vars, array( + 'widget' => $options['widget'], 'multiple' => $options['multiple'], - 'expanded' => $options['expanded'], + 'expanded' => $options['expanded'], // BC 'preferred_choices' => $choiceListView->preferredChoices, 'choices' => $choiceListView->choices, 'separator' => '-------------------', @@ -188,6 +195,10 @@ public function buildView(FormView $view, FormInterface $form, array $options) 'choice_translation_domain' => $choiceTranslationDomain, )); + if (in_array($options['widget'], array('text', 'hidden'))) { + return; + } + // The decision, whether a choice is selected, is potentially done // thousand of times during the rendering of a template. Provide a // closure here that is optimized for the value of the form, to @@ -226,6 +237,10 @@ public function buildView(FormView $view, FormInterface $form, array $options) */ public function finishView(FormView $view, FormInterface $form, array $options) { + if (in_array($options['widget'], array('text', 'hidden'))) { + return; + } + if ($options['expanded']) { // Radio buttons should have the same name as the parent $childName = $view->vars['full_name']; @@ -302,9 +317,30 @@ public function configureOptions(OptionsResolver $resolver) return $choiceTranslationDomain; }; + $multipleNormalizer = function (Options $options, $multiple) { + if (in_array($options['widget'], array('radio', 'checkbox'))) { + return 'checkbox' == $options['widget']; + } + + return $multiple; + }; + + $expandedNomalizer = function (Options $options, $expanded) { + if (null !== $expanded) { + @trigger_error('The "expanded" option is deprecated since version 3.1 and will be removed in 4.0. You should use "widget" instead.', E_USER_DEPRECATED); + } else { + $expanded = false; + } + + return in_array($options['widget'], array('radio', 'checkbox')) ?: $expanded; + }; + $resolver->setDefaults(array( + 'widget' => null, 'multiple' => false, - 'expanded' => false, + 'delimiter' => ',', + 'expanded' => null, // deprecated + 'choice_list' => null, // deprecated 'choices' => array(), 'choices_as_values' => null, // deprecated since 3.1 'choice_loader' => null, @@ -325,6 +361,8 @@ public function configureOptions(OptionsResolver $resolver) 'choice_translation_domain' => true, )); + $resolver->setNormalizer('expanded', $expandedNomalizer); + $resolver->setNormalizer('multiple', $multipleNormalizer); $resolver->setNormalizer('placeholder', $placeholderNormalizer); $resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer); $resolver->setNormalizer('choices_as_values', $choicesAsValuesNormalizer); diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/ValuesToStringTransformerTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/ValuesToStringTransformerTest.php new file mode 100644 index 0000000000000..6105b1e47add1 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/ValuesToStringTransformerTest.php @@ -0,0 +1,71 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer; + +use Symfony\Component\Form\Extension\Core\DataTransformer\ValuesToStringTransformer; + +class ValuesToStringTransformerTest extends \PHPUnit_Framework_TestCase +{ + private $transformer; + + protected function setUp() + { + $this->transformer = new ValuesToStringTransformer(',', true); + } + + protected function tearDown() + { + $this->transformer = null; + } + + public function testTransform() + { + $output = 'a,b,c'; + + $this->assertSame($output, $this->transformer->transform(array('a', 'b', 'c'))); + } + + public function testTransformNull() + { + $this->assertSame('', $this->transformer->transform(null)); + } + + /** + * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException + */ + public function testReverseTransformRequiresAnArray() + { + $this->transformer->transform('a, b, c'); + } + + public function testReverseTransform() + { + $input = 'a, b ,c '; + + $this->assertSame(array('a', 'b', 'c'), $this->transformer->reverseTransform($input)); + } + + public function testReverseTransformEmpty() + { + $input = ''; + + $this->assertSame(array(), $this->transformer->reverseTransform($input)); + } + + /** + * @expectedException \Symfony\Component\Form\Exception\TransformationFailedException + */ + public function testReverseTransformRequiresAString() + { + $this->transformer->reverseTransform(array('a', 'b', 'c')); + } +} 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 a74657d918f72..cbb8ca85ba901 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -1505,4 +1505,68 @@ public function testCustomChoiceTypeDoesNotInheritChoiceLabels() // In this case the 'choice_label' closure returns null and not the closure from the first choice type. $this->assertNull($form->get('subChoice')->getConfig()->getOption('choice_label')); } + + /** + * @dataProvider simpleWidgetsProvider + */ + public function testSubmitChoicesWithSimpleWidgets($widget) + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'widget' => $widget, + 'multiple' => false, + 'choices' => $this->choices, + 'choices_as_values' => true, + )); + + $form->submit('b'); + + $this->assertEquals('b', $form->getData()); + $this->assertEquals('b', $form->getViewData()); + } + + /** + * @dataProvider simpleWidgetsProvider + */ + public function testSubmitMultipleChoicesWithSimpleWidgets($widget) + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'widget' => $widget, + 'multiple' => true, + 'choices' => $this->choices, + 'choices_as_values' => true, + )); + + $form->submit('a,b'); + + $this->assertEquals(array('a', 'b'), $form->getData()); + $this->assertEquals('a,b', $form->getViewData()); + } + + /** + * @dataProvider simpleWidgetsProvider + */ + public function testSubmitMultipleChoicesDelimiterAndTrimWithSimpleWidgets($widget) + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'widget' => $widget, + 'multiple' => true, + 'delimiter' => '|', + 'trim' => true, + 'choices' => $this->choices, + 'choices_as_values' => true, + )); + + $form->submit('a| b '); + + $this->assertEquals(array('a', 'b'), $form->getData()); + $this->assertEquals('a|b', $form->getViewData()); + } + + public function simpleWidgetsProvider() + { + return array( + array('text'), + array('hidden'), + ); + } } From ced878aaebc5fd7e8f58174e09d410762d3c2dc6 Mon Sep 17 00:00:00 2001 From: Bilal Amarni Date: Tue, 2 Feb 2016 18:47:37 +0100 Subject: [PATCH 2/4] fix twig bridge tests - use strict comparison --- .../views/Form/form_div_layout.html.twig | 4 ++-- .../Form/Extension/Core/Type/ChoiceType.php | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig index 84137c96cd5b9..21281005448ef 100644 --- a/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig +++ b/src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig @@ -35,9 +35,9 @@ {%- endblock textarea_widget -%} {%- block choice_widget -%} - {% if 'hidden' == widget %} + {% if widget is defined and 'hidden' == widget %} {{- block('hidden_widget') -}} - {% elseif 'text' == widget %} + {% elseif widget is defined and 'text' == widget %} {{- block('form_widget_simple') -}} {% elseif expanded %} {{- block('choice_widget_expanded') -}} diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index a1414ca6dc3c1..50f2a02e34b44 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -61,7 +61,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) $choiceList = $this->createChoiceList($options); $builder->setAttribute('choice_list', $choiceList); - if ($options['expanded'] && !in_array($options['widget'], array('text', 'hidden'))) { + if ($options['expanded'] && !in_array($options['widget'], array('text', 'hidden'), true)) { $builder->setDataMapper($options['multiple'] ? new CheckboxListMapper($choiceList) : new RadioListMapper($choiceList)); @@ -151,7 +151,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) $builder->addViewTransformer(new ChoicesToValuesTransformer($choiceList)); // for "text" / "hidden" widget, view data uses a delimiter - if (in_array($options['widget'], array('text', 'hidden'))) { + if (in_array($options['widget'], array('text', 'hidden'), true)) { $builder->addViewTransformer(new ValuesToStringTransformer($options['delimiter'], $options['trim'])); } } else { @@ -195,7 +195,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) 'choice_translation_domain' => $choiceTranslationDomain, )); - if (in_array($options['widget'], array('text', 'hidden'))) { + if (in_array($options['widget'], array('text', 'hidden'), true)) { return; } @@ -237,7 +237,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) */ public function finishView(FormView $view, FormInterface $form, array $options) { - if (in_array($options['widget'], array('text', 'hidden'))) { + if (in_array($options['widget'], array('text', 'hidden'), true)) { return; } @@ -318,8 +318,8 @@ public function configureOptions(OptionsResolver $resolver) }; $multipleNormalizer = function (Options $options, $multiple) { - if (in_array($options['widget'], array('radio', 'checkbox'))) { - return 'checkbox' == $options['widget']; + if (in_array($options['widget'], array('radio', 'checkbox'), true)) { + return 'checkbox' === $options['widget']; } return $multiple; @@ -332,7 +332,7 @@ public function configureOptions(OptionsResolver $resolver) $expanded = false; } - return in_array($options['widget'], array('radio', 'checkbox')) ?: $expanded; + return in_array($options['widget'], array('radio', 'checkbox'), true) ?: $expanded; }; $resolver->setDefaults(array( @@ -376,6 +376,7 @@ 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', 'callable', 'string', 'Symfony\Component\PropertyAccess\PropertyPath')); + $resolver->setAllowedValues('widget', array(null, 'hidden', 'text', 'select', 'checkbox', 'radio')); } /** From 1491bb7a43796b46b80a75b5b93224c959afc6f1 Mon Sep 17 00:00:00 2001 From: Bilal Amarni Date: Tue, 2 Feb 2016 19:12:45 +0100 Subject: [PATCH 3/4] add legacy tag to EntityTypeTest - edit upgrade/changelog --- UPGRADE-4.0.md | 38 +++++++++++++++++++ .../Tests/Form/Type/EntityTypeTest.php | 3 ++ src/Symfony/Component/Form/CHANGELOG.md | 1 + 3 files changed, 42 insertions(+) diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index 2d402d75fbb88..5a0bf630ba61c 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -14,6 +14,44 @@ Form * The `choices_as_values` option of the `ChoiceType` has been removed. + * The `expanded` option of the `ChoiceType` has been removed. + Use instead the `widget` option with `select`, `checkbox`, `radio`, `text` or `hidden`. + + Before: + + ```php + use Symfony\Component\Form\Extension\Core\Type\ChoiceType; + + $form = $this->createFormBuilder() + ->add('isAttending', ChoiceType::class, array( + 'choices' => array( + 'Maybe' => null, + 'Yes' => true, + 'No' => false, + ), + 'expanded' => true, + 'multiple' => true, + )) + ->getForm(); + ``` + + After: + + ```php + use Symfony\Component\Form\Extension\Core\Type\ChoiceType; + + $form = $this->createFormBuilder() + ->add('isAttending', ChoiceType::class, array( + 'choices' => array( + 'Maybe' => null, + 'Yes' => true, + 'No' => false, + ), + 'widget' => 'checkbox', + )) + ->getForm(); + ``` + Serializer ---------- diff --git a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php index 0303c355c055b..07fe77b564d60 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php @@ -34,6 +34,9 @@ use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleAssociationToIntIdEntity; use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdNoToStringEntity; +/** + * @group legacy + */ class EntityTypeTest extends TypeTestCase { const ITEM_GROUP_CLASS = 'Symfony\Bridge\Doctrine\Tests\Fixtures\GroupableEntity'; diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 3671caab0898a..882e36731ec71 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * deprecated the "choices_as_values" option of ChoiceType + * deprecated the "expanded" option of ChoiceType 3.0.0 ----- From 2a972f3264c59366c13f567b18d01eb515c0f93e Mon Sep 17 00:00:00 2001 From: Bilal Amarni Date: Wed, 3 Feb 2016 23:11:52 +0100 Subject: [PATCH 4/4] update tests --- UPGRADE-4.0.md | 6 +- .../Tests/AbstractBootstrap3LayoutTest.php | 63 +- .../Form/Tests/AbstractDivLayoutTest.php | 2 +- .../Form/Tests/AbstractLayoutTest.php | 63 +- .../Extension/Core/Type/ChoiceTypeTest.php | 156 +-- .../Core/Type/LegacyChoiceTypeTest.php | 1161 +++++++++++++++++ .../Form/Tests/Fixtures/ChoiceSubType.php | 2 +- 7 files changed, 1281 insertions(+), 172 deletions(-) create mode 100644 src/Symfony/Component/Form/Tests/Extension/Core/Type/LegacyChoiceTypeTest.php diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index 5a0bf630ba61c..70f98fd709fa1 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -25,12 +25,11 @@ Form $form = $this->createFormBuilder() ->add('isAttending', ChoiceType::class, array( 'choices' => array( - 'Maybe' => null, 'Yes' => true, 'No' => false, ), 'expanded' => true, - 'multiple' => true, + 'multiple' => false, )) ->getForm(); ``` @@ -43,11 +42,10 @@ Form $form = $this->createFormBuilder() ->add('isAttending', ChoiceType::class, array( 'choices' => array( - 'Maybe' => null, 'Yes' => true, 'No' => false, ), - 'widget' => 'checkbox', + 'widget' => 'radio', )) ->getForm(); ``` diff --git a/src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php index 32a4af7dde8c6..616a4c2574829 100644 --- a/src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php @@ -214,7 +214,7 @@ public function testSingleChoice() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')), @@ -236,7 +236,7 @@ public function testSingleChoiceWithoutTranslation() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choice_translation_domain' => false, )); @@ -259,7 +259,7 @@ public function testSingleChoiceWithPlaceholderWithoutTranslation() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'required' => false, 'translation_domain' => false, 'placeholder' => 'Placeholder&Not&Translated', @@ -286,7 +286,7 @@ public function testSingleChoiceAttributes() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'choice_attr' => array('Choice&B' => array('class' => 'foo&bar')), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')), @@ -309,7 +309,7 @@ public function testSingleChoiceWithPreferred() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'preferred_choices' => array('&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('separator' => '-- sep --', 'attr' => array('class' => 'my&class')), @@ -333,7 +333,7 @@ public function testSingleChoiceWithPreferredAndNoSeparator() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'preferred_choices' => array('&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('separator' => null, 'attr' => array('class' => 'my&class')), @@ -356,7 +356,7 @@ public function testSingleChoiceWithPreferredAndBlankSeparator() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'preferred_choices' => array('&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('separator' => '', 'attr' => array('class' => 'my&class')), @@ -380,7 +380,7 @@ public function testChoiceWithOnlyPreferred() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'preferred_choices' => array('&a', '&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')), @@ -397,7 +397,7 @@ public function testSingleChoiceNonRequired() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => false, 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')), @@ -421,7 +421,7 @@ public function testSingleChoiceNonRequiredNoneSelected() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => false, 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')), @@ -444,7 +444,7 @@ public function testSingleChoiceNonRequiredWithPlaceholder() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'required' => false, 'placeholder' => 'Select&Anything&Not&Me', )); @@ -470,7 +470,7 @@ public function testSingleChoiceRequiredWithPlaceholder() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => true, 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'placeholder' => 'Test&Me', )); @@ -495,7 +495,7 @@ public function testSingleChoiceRequiredWithPlaceholderViaView() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => true, 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('placeholder' => '', 'attr' => array('class' => 'my&class')), @@ -521,7 +521,7 @@ public function testSingleChoiceGrouped() 'Group&2' => array('Choice&C' => '&c'), ), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')), @@ -550,7 +550,7 @@ public function testMultipleChoice() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => true, 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')), @@ -575,7 +575,7 @@ public function testMultipleChoiceAttributes() 'choice_attr' => array('Choice&B' => array('class' => 'foo&bar')), 'required' => true, 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')), @@ -598,7 +598,7 @@ public function testMultipleChoiceSkipsPlaceholder() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a'), array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', 'placeholder' => 'Test&Me', )); @@ -622,7 +622,7 @@ public function testMultipleChoiceNonRequired() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => false, 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')), @@ -643,8 +643,7 @@ public function testSingleChoiceExpanded() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -678,8 +677,7 @@ public function testSingleChoiceExpandedWithoutTranslation() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'choice_translation_domain' => false, )); @@ -715,8 +713,7 @@ public function testSingleChoiceExpandedAttributes() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'choice_attr' => array('Choice&B' => array('class' => 'foo&bar')), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -750,8 +747,7 @@ public function testSingleChoiceExpandedWithPlaceholder() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'placeholder' => 'Test&Me', )); @@ -795,8 +791,7 @@ public function testSingleChoiceExpandedWithPlaceholderWithoutTranslation() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'translation_domain' => false, 'placeholder' => 'Placeholder&Not&Translated', )); @@ -841,8 +836,7 @@ public function testSingleChoiceExpandedWithBooleanValue() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', true, array( 'choices' => array('Choice&A' => '1', 'Choice&B' => '0'), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -876,8 +870,7 @@ public function testMultipleChoiceExpanded() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a', '&c'), array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'), - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'required' => true, )); @@ -921,8 +914,7 @@ public function testMultipleChoiceExpandedWithoutTranslation() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a', '&c'), array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'), - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'required' => true, 'choice_translation_domain' => false, )); @@ -968,8 +960,7 @@ public function testMultipleChoiceExpandedAttributes() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a', '&c'), array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'), 'choice_attr' => array('Choice&B' => array('class' => 'foo&bar')), - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'required' => true, )); diff --git a/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php index b5f6017084369..c73d97f566025 100644 --- a/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php @@ -692,7 +692,7 @@ public function testChoiceRowWithCustomBlock() { $form = $this->factory->createNamedBuilder('name_c', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', 'a', array( 'choices' => array('ChoiceA' => 'a', 'ChoiceB' => 'b'), - 'expanded' => true, + 'widget' => 'radio', )) ->getForm(); diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index 7bade490f9b2b..d055a88a8c2f6 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -489,7 +489,7 @@ public function testSingleChoice() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); // If the field is collapsed, has no "multiple" attribute, is required but @@ -522,7 +522,7 @@ public function testSingleChoiceWithoutTranslation() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choice_translation_domain' => false, )); @@ -544,7 +544,7 @@ public function testSingleChoiceWithPlaceholderWithoutTranslation() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'required' => false, 'translation_domain' => false, 'placeholder' => 'Placeholder&Not&Translated', @@ -570,7 +570,7 @@ public function testSingleChoiceAttributes() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'choice_attr' => array('Choice&B' => array('class' => 'foo&bar')), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -592,7 +592,7 @@ public function testSingleChoiceWithPreferred() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'preferred_choices' => array('&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('separator' => '-- sep --'), @@ -615,7 +615,7 @@ public function testSingleChoiceWithPreferredAndNoSeparator() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'preferred_choices' => array('&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('separator' => null), @@ -637,7 +637,7 @@ public function testSingleChoiceWithPreferredAndBlankSeparator() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'preferred_choices' => array('&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array('separator' => ''), @@ -660,7 +660,7 @@ public function testChoiceWithOnlyPreferred() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'preferred_choices' => array('&a', '&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -676,7 +676,7 @@ public function testSingleChoiceNonRequired() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => false, 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -699,7 +699,7 @@ public function testSingleChoiceNonRequiredNoneSelected() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => false, 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -721,7 +721,7 @@ public function testSingleChoiceNonRequiredWithPlaceholder() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'required' => false, 'placeholder' => 'Select&Anything&Not&Me', )); @@ -746,7 +746,7 @@ public function testSingleChoiceRequiredWithPlaceholder() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => true, 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'placeholder' => 'Test&Me', )); @@ -773,7 +773,7 @@ public function testSingleChoiceRequiredWithPlaceholderViaView() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => true, 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); // The "disabled" attribute was removed again due to a bug in the @@ -801,7 +801,7 @@ public function testSingleChoiceGrouped() 'Group&2' => array('Choice&C' => '&c'), ), 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -829,7 +829,7 @@ public function testMultipleChoice() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => true, 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -853,7 +853,7 @@ public function testMultipleChoiceAttributes() 'choice_attr' => array('Choice&B' => array('class' => 'foo&bar')), 'required' => true, 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -875,7 +875,7 @@ public function testMultipleChoiceSkipsPlaceholder() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a'), array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', 'placeholder' => 'Test&Me', )); @@ -898,7 +898,7 @@ public function testMultipleChoiceNonRequired() 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'required' => false, 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -918,8 +918,7 @@ public function testSingleChoiceExpanded() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -940,8 +939,7 @@ public function testSingleChoiceExpandedWithoutTranslation() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'choice_translation_domain' => false, )); @@ -964,8 +962,7 @@ public function testSingleChoiceExpandedAttributes() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), 'choice_attr' => array('Choice&B' => array('class' => 'foo&bar')), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -986,8 +983,7 @@ public function testSingleChoiceExpandedWithPlaceholder() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'placeholder' => 'Test&Me', )); @@ -1011,8 +1007,7 @@ public function testSingleChoiceExpandedWithPlaceholderWithoutTranslation() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', '&a', array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b'), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'translation_domain' => false, 'placeholder' => 'Placeholder&Not&Translated', )); @@ -1037,8 +1032,7 @@ public function testSingleChoiceExpandedWithBooleanValue() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', true, array( 'choices' => array('Choice&A' => '1', 'Choice&B' => '0'), - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -1059,8 +1053,7 @@ public function testMultipleChoiceExpanded() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a', '&c'), array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'), - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'required' => true, )); @@ -1084,8 +1077,7 @@ public function testMultipleChoiceExpandedWithoutTranslation() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a', '&c'), array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'), - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'required' => true, 'choice_translation_domain' => false, )); @@ -1111,8 +1103,7 @@ public function testMultipleChoiceExpandedAttributes() $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', array('&a', '&c'), array( 'choices' => array('Choice&A' => '&a', 'Choice&B' => '&b', 'Choice&C' => '&c'), 'choice_attr' => array('Choice&B' => array('class' => 'foo&bar')), - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'required' => true, )); 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 cbb8ca85ba901..1a7839ecb773b 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -13,8 +13,9 @@ use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView; use Symfony\Component\Form\ChoiceList\View\ChoiceView; +use Symfony\Component\Form\Test\TypeTestCase; -class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase +class ChoiceTypeTest extends TypeTestCase { private $choices = array( 'Bernhard' => 'a', @@ -87,7 +88,7 @@ public function testChoiceListAndChoicesCanBeEmpty() public function testExpandedChoicesOptionsTurnIntoChildren() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'expanded' => true, + 'widget' => 'radio', 'choices' => $this->choices, )); @@ -97,8 +98,7 @@ public function testExpandedChoicesOptionsTurnIntoChildren() public function testPlaceholderPresentOnNonRequiredExpandedSingleChoice() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -110,8 +110,7 @@ public function testPlaceholderPresentOnNonRequiredExpandedSingleChoice() public function testPlaceholderNotPresentIfRequired() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -123,8 +122,7 @@ public function testPlaceholderNotPresentIfRequired() public function testPlaceholderNotPresentIfMultiple() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'required' => false, 'choices' => $this->choices, )); @@ -136,8 +134,7 @@ public function testPlaceholderNotPresentIfMultiple() public function testPlaceholderNotPresentIfEmptyChoice() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => false, 'choices' => array( 'Empty' => '', @@ -152,7 +149,7 @@ public function testPlaceholderNotPresentIfEmptyChoice() public function testExpandedChoicesOptionsAreFlattened() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'expanded' => true, + 'widget' => 'radio', 'choices' => $this->groupedChoices, )); @@ -177,7 +174,7 @@ public function testExpandedChoicesOptionsAreFlattenedObjectChoices() $obj5 = (object) array('id' => 5, 'name' => 'Roman'); $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'expanded' => true, + 'widget' => 'radio', 'choices' => array( 'Symfony' => array($obj1, $obj2, $obj3), 'Doctrine' => array($obj4, $obj5), @@ -196,8 +193,7 @@ public function testExpandedChoicesOptionsAreFlattenedObjectChoices() public function testExpandedCheckboxesAreNeverRequired() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'required' => true, 'choices' => $this->choices, )); @@ -210,8 +206,7 @@ public function testExpandedCheckboxesAreNeverRequired() public function testExpandedRadiosAreRequiredIfChoiceChildIsRequired() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -224,8 +219,7 @@ public function testExpandedRadiosAreRequiredIfChoiceChildIsRequired() public function testExpandedRadiosAreNotRequiredIfChoiceChildIsNotRequired() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -239,7 +233,7 @@ public function testSubmitSingleNonExpanded() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->choices, )); @@ -254,7 +248,7 @@ public function testSubmitSingleNonExpandedInvalidChoice() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->choices, )); @@ -269,7 +263,7 @@ public function testSubmitSingleNonExpandedNull() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->choices, )); @@ -287,7 +281,7 @@ public function testSubmitSingleNonExpandedNullNoChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choices' => array(), )); @@ -302,7 +296,7 @@ public function testSubmitSingleNonExpandedEmpty() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->choices, )); @@ -317,7 +311,7 @@ public function testSubmitSingleNonExpandedEmptyExplicitEmptyChoice() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choices' => array( 'Empty' => 'EMPTY_CHOICE', ), @@ -340,7 +334,7 @@ public function testSubmitSingleNonExpandedEmptyNoChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choices' => array(), )); @@ -355,7 +349,7 @@ public function testSubmitSingleNonExpandedFalse() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->choices, )); @@ -373,7 +367,7 @@ public function testSubmitSingleNonExpandedFalseNoChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choices' => array(), )); @@ -388,7 +382,7 @@ public function testSubmitSingleNonExpandedObjectChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->objectChoices, 'choice_label' => 'name', 'choice_value' => 'id', @@ -406,7 +400,7 @@ public function testSubmitMultipleNonExpanded() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->choices, )); @@ -421,7 +415,7 @@ public function testSubmitMultipleNonExpandedEmpty() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->choices, )); @@ -439,7 +433,7 @@ public function testSubmitMultipleNonExpandedEmptyNoChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', 'choices' => array(), )); @@ -454,7 +448,7 @@ public function testSubmitMultipleNonExpandedInvalidScalarChoice() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->choices, )); @@ -469,7 +463,7 @@ public function testSubmitMultipleNonExpandedInvalidArrayChoice() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->choices, )); @@ -484,7 +478,7 @@ public function testSubmitMultipleNonExpandedObjectChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->objectChoices, 'choice_label' => 'name', 'choice_value' => 'id', @@ -500,8 +494,7 @@ public function testSubmitMultipleNonExpandedObjectChoices() public function testSubmitSingleExpandedRequired() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -528,8 +521,7 @@ public function testSubmitSingleExpandedRequired() public function testSubmitSingleExpandedRequiredInvalidChoice() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -556,8 +548,7 @@ public function testSubmitSingleExpandedRequiredInvalidChoice() public function testSubmitSingleExpandedNonRequired() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -586,8 +577,7 @@ public function testSubmitSingleExpandedNonRequired() public function testSubmitSingleExpandedNonRequiredInvalidChoice() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -614,8 +604,7 @@ public function testSubmitSingleExpandedNonRequiredInvalidChoice() public function testSubmitSingleExpandedRequiredNull() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -645,8 +634,7 @@ public function testSubmitSingleExpandedRequiredNull() public function testSubmitSingleExpandedRequiredNullNoChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => true, 'choices' => array(), )); @@ -662,8 +650,7 @@ public function testSubmitSingleExpandedRequiredNullNoChoices() public function testSubmitSingleExpandedRequiredEmpty() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -693,8 +680,7 @@ public function testSubmitSingleExpandedRequiredEmpty() public function testSubmitSingleExpandedRequiredEmptyNoChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => true, 'choices' => array(), )); @@ -710,8 +696,7 @@ public function testSubmitSingleExpandedRequiredEmptyNoChoices() public function testSubmitSingleExpandedRequiredFalse() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => true, 'choices' => $this->choices, )); @@ -741,8 +726,7 @@ public function testSubmitSingleExpandedRequiredFalse() public function testSubmitSingleExpandedRequiredFalseNoChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => true, 'choices' => array(), )); @@ -758,8 +742,7 @@ public function testSubmitSingleExpandedRequiredFalseNoChoices() public function testSubmitSingleExpandedNonRequiredNull() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -791,8 +774,7 @@ public function testSubmitSingleExpandedNonRequiredNull() public function testSubmitSingleExpandedNonRequiredNullNoChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => false, 'choices' => array(), )); @@ -808,8 +790,7 @@ public function testSubmitSingleExpandedNonRequiredNullNoChoices() public function testSubmitSingleExpandedNonRequiredEmpty() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -841,8 +822,7 @@ public function testSubmitSingleExpandedNonRequiredEmpty() public function testSubmitSingleExpandedNonRequiredEmptyNoChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => false, 'choices' => array(), )); @@ -858,8 +838,7 @@ public function testSubmitSingleExpandedNonRequiredEmptyNoChoices() public function testSubmitSingleExpandedNonRequiredFalse() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => false, 'choices' => $this->choices, )); @@ -891,8 +870,7 @@ public function testSubmitSingleExpandedNonRequiredFalse() public function testSubmitSingleExpandedNonRequiredFalseNoChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'required' => false, 'choices' => array(), )); @@ -908,8 +886,7 @@ public function testSubmitSingleExpandedNonRequiredFalseNoChoices() public function testSubmitSingleExpandedWithEmptyChild() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'choices' => array( 'Empty' => '', 'Not empty' => 1, @@ -930,8 +907,7 @@ public function testSubmitSingleExpandedWithEmptyChild() public function testSubmitSingleExpandedObjectChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => false, - 'expanded' => true, + 'widget' => 'radio', 'choices' => $this->objectChoices, 'choice_label' => 'name', 'choice_value' => 'id', @@ -957,8 +933,7 @@ public function testSubmitSingleExpandedObjectChoices() public function testSubmitMultipleExpanded() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'choices' => $this->choices, )); @@ -984,8 +959,7 @@ public function testSubmitMultipleExpanded() public function testSubmitMultipleExpandedInvalidScalarChoice() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'choices' => $this->choices, )); @@ -1011,8 +985,7 @@ public function testSubmitMultipleExpandedInvalidScalarChoice() public function testSubmitMultipleExpandedInvalidArrayChoice() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'choices' => $this->choices, )); @@ -1038,8 +1011,7 @@ public function testSubmitMultipleExpandedInvalidArrayChoice() public function testSubmitMultipleExpandedEmpty() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'choices' => $this->choices, )); @@ -1066,8 +1038,7 @@ public function testSubmitMultipleExpandedEmpty() public function testSubmitMultipleExpandedEmptyNoChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'choices' => array(), )); @@ -1080,8 +1051,7 @@ public function testSubmitMultipleExpandedEmptyNoChoices() public function testSubmitMultipleExpandedWithEmptyChild() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'choices' => array( 'Empty' => '', 'Not Empty' => 1, @@ -1105,8 +1075,7 @@ public function testSubmitMultipleExpandedWithEmptyChild() public function testSubmitMultipleExpandedObjectChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'multiple' => true, - 'expanded' => true, + 'widget' => 'checkbox', 'choices' => $this->objectChoices, 'choice_label' => 'name', 'choice_value' => 'id', @@ -1133,7 +1102,7 @@ public function testSingleSelectedObjectChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', $this->objectChoices[3], array( 'multiple' => false, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->objectChoices, 'choice_label' => 'name', 'choice_value' => 'id', @@ -1150,7 +1119,7 @@ public function testMultipleSelectedObjectChoices() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', array($this->objectChoices[3]), array( 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->objectChoices, 'choice_label' => 'name', 'choice_value' => 'id', @@ -1198,7 +1167,7 @@ public function testPassMultipleToView() public function testPassExpandedToView() { $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( - 'expanded' => true, + 'widget' => 'radio', 'choices' => $this->choices, )); $view = $form->createView(); @@ -1278,7 +1247,8 @@ public function testPlaceholderIsEmptyStringByDefaultIfNotRequired() } /** - * @dataProvider getOptionsWithPlaceholder + * @dataProvider getLegacyOptionsWithPlaceholder + * @group legacy */ public function testPassPlaceholderToView($multiple, $expanded, $required, $placeholder, $viewValue) { @@ -1296,7 +1266,8 @@ public function testPassPlaceholderToView($multiple, $expanded, $required, $plac } /** - * @dataProvider getOptionsWithPlaceholder + * @dataProvider getLegacyOptionsWithPlaceholder + * @group legacy */ public function testDontPassPlaceholderIfContainedInChoices($multiple, $expanded, $required, $placeholder, $viewValue) { @@ -1313,7 +1284,7 @@ public function testDontPassPlaceholderIfContainedInChoices($multiple, $expanded $this->assertTrue($view->vars['placeholder_in_choices']); } - public function getOptionsWithPlaceholder() + public function getLegacyOptionsWithPlaceholder() { return array( // single non-expanded @@ -1444,7 +1415,7 @@ public function testAdjustFullNameForMultipleNonExpanded() { $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( 'multiple' => true, - 'expanded' => false, + 'widget' => 'select', 'choices' => $this->choices, )); $view = $form->createView(); @@ -1515,7 +1486,6 @@ public function testSubmitChoicesWithSimpleWidgets($widget) 'widget' => $widget, 'multiple' => false, 'choices' => $this->choices, - 'choices_as_values' => true, )); $form->submit('b'); @@ -1533,7 +1503,6 @@ public function testSubmitMultipleChoicesWithSimpleWidgets($widget) 'widget' => $widget, 'multiple' => true, 'choices' => $this->choices, - 'choices_as_values' => true, )); $form->submit('a,b'); @@ -1553,7 +1522,6 @@ public function testSubmitMultipleChoicesDelimiterAndTrimWithSimpleWidgets($widg 'delimiter' => '|', 'trim' => true, 'choices' => $this->choices, - 'choices_as_values' => true, )); $form->submit('a| b '); diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/LegacyChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/LegacyChoiceTypeTest.php new file mode 100644 index 0000000000000..4e9ed27187ae2 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/LegacyChoiceTypeTest.php @@ -0,0 +1,1161 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests\Extension\Core\Type; + +use Symfony\Component\Form\Test\TypeTestCase; + +class LegacyChoiceTypeTest extends TypeTestCase +{ + private $choices = array( + 'Bernhard' => 'a', + 'Fabien' => 'b', + 'Kris' => 'c', + 'Jon' => 'd', + 'Roman' => 'e', + ); + + private $objectChoices; + + protected $groupedChoices = array( + 'Symfony' => array( + 'Bernhard' => 'a', + 'Fabien' => 'b', + 'Kris' => 'c', + ), + 'Doctrine' => array( + 'Jon' => 'd', + 'Roman' => 'e', + ), + ); + + protected function setUp() + { + parent::setUp(); + + $this->objectChoices = array( + (object) array('id' => 1, 'name' => 'Bernhard'), + (object) array('id' => 2, 'name' => 'Fabien'), + (object) array('id' => 3, 'name' => 'Kris'), + (object) array('id' => 4, 'name' => 'Jon'), + (object) array('id' => 5, 'name' => 'Roman'), + ); + } + + protected function tearDown() + { + parent::tearDown(); + + $this->objectChoices = null; + } + + public function testExpandedChoicesOptionsTurnIntoChildren() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'expanded' => true, + 'choices' => $this->choices, + )); + + $this->assertCount(count($this->choices), $form, 'Each choice should become a new field'); + } + + public function testPlaceholderPresentOnNonRequiredExpandedSingleChoice() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => $this->choices, + )); + + $this->assertTrue(isset($form['placeholder'])); + $this->assertCount(count($this->choices) + 1, $form, 'Each choice should become a new field'); + } + + public function testPlaceholderNotPresentIfRequired() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => $this->choices, + )); + + $this->assertFalse(isset($form['placeholder'])); + $this->assertCount(count($this->choices), $form, 'Each choice should become a new field'); + } + + public function testPlaceholderNotPresentIfMultiple() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => true, + 'required' => false, + 'choices' => $this->choices, + )); + + $this->assertFalse(isset($form['placeholder'])); + $this->assertCount(count($this->choices), $form, 'Each choice should become a new field'); + } + + public function testPlaceholderNotPresentIfEmptyChoice() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => array( + 'Empty' => '', + 'Not empty' => 1, + ), + )); + + $this->assertFalse(isset($form['placeholder'])); + $this->assertCount(2, $form, 'Each choice should become a new field'); + } + + public function testExpandedChoicesOptionsAreFlattened() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'expanded' => true, + 'choices' => $this->groupedChoices, + )); + + $flattened = array(); + foreach ($this->groupedChoices as $choices) { + $flattened = array_merge($flattened, array_keys($choices)); + } + + $this->assertCount($form->count(), $flattened, 'Each nested choice should become a new field, not the groups'); + + foreach ($flattened as $value => $choice) { + $this->assertTrue($form->has($value), 'Flattened choice is named after it\'s value'); + } + } + + public function testExpandedChoicesOptionsAreFlattenedObjectChoices() + { + $obj1 = (object) array('id' => 1, 'name' => 'Bernhard'); + $obj2 = (object) array('id' => 2, 'name' => 'Fabien'); + $obj3 = (object) array('id' => 3, 'name' => 'Kris'); + $obj4 = (object) array('id' => 4, 'name' => 'Jon'); + $obj5 = (object) array('id' => 5, 'name' => 'Roman'); + + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'expanded' => true, + 'choices' => array( + 'Symfony' => array($obj1, $obj2, $obj3), + 'Doctrine' => array($obj4, $obj5), + ), + 'choice_name' => 'id', + )); + + $this->assertSame(5, $form->count(), 'Each nested choice should become a new field, not the groups'); + $this->assertTrue($form->has(1)); + $this->assertTrue($form->has(2)); + $this->assertTrue($form->has(3)); + $this->assertTrue($form->has(4)); + $this->assertTrue($form->has(5)); + } + + public function testExpandedCheckboxesAreNeverRequired() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => true, + 'required' => true, + 'choices' => $this->choices, + )); + + foreach ($form as $child) { + $this->assertFalse($child->isRequired()); + } + } + + public function testExpandedRadiosAreRequiredIfChoiceChildIsRequired() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => $this->choices, + )); + + foreach ($form as $child) { + $this->assertTrue($child->isRequired()); + } + } + + public function testExpandedRadiosAreNotRequiredIfChoiceChildIsNotRequired() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => $this->choices, + )); + + foreach ($form as $child) { + $this->assertFalse($child->isRequired()); + } + } + + public function testSubmitSingleNonExpanded() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->submit('b'); + + $this->assertEquals('b', $form->getData()); + $this->assertEquals('b', $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleNonExpandedInvalidChoice() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->submit('foobar'); + + $this->assertNull($form->getData()); + $this->assertEquals('foobar', $form->getViewData()); + $this->assertFalse($form->isSynchronized()); + } + + public function testSubmitSingleNonExpandedNull() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->submit(null); + + $this->assertNull($form->getData()); + $this->assertSame('', $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + // In edge cases (for example, when choices are loaded dynamically by a + // loader), the choices may be empty. Make sure to behave the same as when + // choices are available. + public function testSubmitSingleNonExpandedNullNoChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => false, + 'choices' => array(), + )); + + $form->submit(null); + + $this->assertNull($form->getData()); + $this->assertSame('', $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleNonExpandedEmpty() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->submit(''); + + $this->assertNull($form->getData()); + $this->assertSame('', $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleNonExpandedEmptyExplicitEmptyChoice() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => false, + 'choices' => array( + 'Empty' => 'EMPTY_CHOICE', + ), + 'choice_value' => function () { + return ''; + }, + )); + + $form->submit(''); + + $this->assertSame('EMPTY_CHOICE', $form->getData()); + $this->assertSame('', $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + // In edge cases (for example, when choices are loaded dynamically by a + // loader), the choices may be empty. Make sure to behave the same as when + // choices are available. + public function testSubmitSingleNonExpandedEmptyNoChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => false, + 'choices' => array(), + )); + + $form->submit(''); + + $this->assertNull($form->getData()); + $this->assertSame('', $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleNonExpandedFalse() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->submit(false); + + $this->assertNull($form->getData()); + $this->assertSame('', $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + // In edge cases (for example, when choices are loaded dynamically by a + // loader), the choices may be empty. Make sure to behave the same as when + // choices are available. + public function testSubmitSingleNonExpandedFalseNoChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => false, + 'choices' => array(), + )); + + $form->submit(false); + + $this->assertNull($form->getData()); + $this->assertSame('', $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleNonExpandedObjectChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => false, + 'choices' => $this->objectChoices, + 'choice_label' => 'name', + 'choice_value' => 'id', + )); + + // "id" value of the second entry + $form->submit('2'); + + $this->assertEquals($this->objectChoices[1], $form->getData()); + $this->assertEquals('2', $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitMultipleNonExpanded() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->submit(array('a', 'b')); + + $this->assertEquals(array('a', 'b'), $form->getData()); + $this->assertEquals(array('a', 'b'), $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitMultipleNonExpandedEmpty() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->submit(array()); + + $this->assertSame(array(), $form->getData()); + $this->assertSame(array(), $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + // In edge cases (for example, when choices are loaded dynamically by a + // loader), the choices may be empty. Make sure to behave the same as when + // choices are available. + public function testSubmitMultipleNonExpandedEmptyNoChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => false, + 'choices' => array(), + )); + + $form->submit(array()); + + $this->assertSame(array(), $form->getData()); + $this->assertSame(array(), $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitMultipleNonExpandedInvalidScalarChoice() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->submit('foobar'); + + $this->assertNull($form->getData()); + $this->assertEquals('foobar', $form->getViewData()); + $this->assertFalse($form->isSynchronized()); + } + + public function testSubmitMultipleNonExpandedInvalidArrayChoice() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => false, + 'choices' => $this->choices, + )); + + $form->submit(array('a', 'foobar')); + + $this->assertNull($form->getData()); + $this->assertEquals(array('a', 'foobar'), $form->getViewData()); + $this->assertFalse($form->isSynchronized()); + } + + public function testSubmitMultipleNonExpandedObjectChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => false, + 'choices' => $this->objectChoices, + 'choice_label' => 'name', + 'choice_value' => 'id', + )); + + $form->submit(array('2', '3')); + + $this->assertEquals(array($this->objectChoices[1], $this->objectChoices[2]), $form->getData()); + $this->assertEquals(array('2', '3'), $form->getViewData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleExpandedRequired() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => $this->choices, + )); + + $form->submit('b'); + + $this->assertSame('b', $form->getData()); + $this->assertSame('b', $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertTrue($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertSame('b', $form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testSubmitSingleExpandedRequiredInvalidChoice() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => $this->choices, + )); + + $form->submit('foobar'); + + $this->assertNull($form->getData()); + $this->assertSame('foobar', $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertFalse($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testSubmitSingleExpandedNonRequired() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => $this->choices, + )); + + $form->submit('b'); + + $this->assertSame('b', $form->getData()); + $this->assertSame('b', $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form['placeholder']->getData()); + $this->assertFalse($form[0]->getData()); + $this->assertTrue($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form['placeholder']->getViewData()); + $this->assertNull($form[0]->getViewData()); + $this->assertSame('b', $form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testSubmitSingleExpandedNonRequiredInvalidChoice() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => $this->choices, + )); + + $form->submit('foobar'); + + $this->assertNull($form->getData()); + $this->assertSame('foobar', $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertFalse($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testSubmitSingleExpandedRequiredNull() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => $this->choices, + )); + + $form->submit(null); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + // In edge cases (for example, when choices are loaded dynamically by a + // loader), the choices may be empty. Make sure to behave the same as when + // choices are available. + public function testSubmitSingleExpandedRequiredNullNoChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => array(), + )); + + $form->submit(null); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleExpandedRequiredEmpty() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => $this->choices, + )); + + $form->submit(''); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + // In edge cases (for example, when choices are loaded dynamically by a + // loader), the choices may be empty. Make sure to behave the same as when + // choices are available. + public function testSubmitSingleExpandedRequiredEmptyNoChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => array(), + )); + + $form->submit(''); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleExpandedRequiredFalse() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => $this->choices, + )); + + $form->submit(false); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + // In edge cases (for example, when choices are loaded dynamically by a + // loader), the choices may be empty. Make sure to behave the same as when + // choices are available. + public function testSubmitSingleExpandedRequiredFalseNoChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => true, + 'choices' => array(), + )); + + $form->submit(false); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleExpandedNonRequiredNull() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => $this->choices, + )); + + $form->submit(null); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertTrue($form['placeholder']->getData()); + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertSame('', $form['placeholder']->getViewData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + // In edge cases (for example, when choices are loaded dynamically by a + // loader), the choices may be empty. Make sure to behave the same as when + // choices are available. + public function testSubmitSingleExpandedNonRequiredNullNoChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => array(), + )); + + $form->submit(null); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleExpandedNonRequiredEmpty() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => $this->choices, + )); + + $form->submit(''); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertTrue($form['placeholder']->getData()); + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertSame('', $form['placeholder']->getViewData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + // In edge cases (for example, when choices are loaded dynamically by a + // loader), the choices may be empty. Make sure to behave the same as when + // choices are available. + public function testSubmitSingleExpandedNonRequiredEmptyNoChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => array(), + )); + + $form->submit(''); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleExpandedNonRequiredFalse() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => $this->choices, + )); + + $form->submit(false); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertTrue($form['placeholder']->getData()); + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertSame('', $form['placeholder']->getViewData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + // In edge cases (for example, when choices are loaded dynamically by a + // loader), the choices may be empty. Make sure to behave the same as when + // choices are available. + public function testSubmitSingleExpandedNonRequiredFalseNoChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'required' => false, + 'choices' => array(), + )); + + $form->submit(false); + + $this->assertNull($form->getData()); + $this->assertNull($form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitSingleExpandedWithEmptyChild() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'choices' => array( + 'Empty' => '', + 'Not empty' => 1, + ), + )); + + $form->submit(''); + + $this->assertNull($form->getData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertTrue($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertSame('', $form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + } + + public function testSubmitSingleExpandedObjectChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => false, + 'expanded' => true, + 'choices' => $this->objectChoices, + 'choice_label' => 'name', + 'choice_value' => 'id', + )); + + $form->submit('2'); + + $this->assertSame($this->objectChoices[1], $form->getData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertTrue($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertSame('2', $form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testSubmitMultipleExpanded() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => true, + 'choices' => $this->choices, + )); + + $form->submit(array('a', 'c')); + + $this->assertSame(array('a', 'c'), $form->getData()); + $this->assertSame(array('a', 'c'), $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertTrue($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertTrue($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertSame('a', $form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertSame('c', $form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testSubmitMultipleExpandedInvalidScalarChoice() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => true, + 'choices' => $this->choices, + )); + + $form->submit('foobar'); + + $this->assertNull($form->getData()); + $this->assertSame('foobar', $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertFalse($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testSubmitMultipleExpandedInvalidArrayChoice() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => true, + 'choices' => $this->choices, + )); + + $form->submit(array('a', 'foobar')); + + $this->assertNull($form->getData()); + $this->assertSame(array('a', 'foobar'), $form->getViewData()); + $this->assertEmpty($form->getExtraData()); + $this->assertFalse($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testSubmitMultipleExpandedEmpty() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => true, + 'choices' => $this->choices, + )); + + $form->submit(array()); + + $this->assertSame(array(), $form->getData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertFalse($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertNull($form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + // In edge cases (for example, when choices are loaded dynamically by a + // loader), the choices may be empty. Make sure to behave the same as when + // choices are available. + public function testSubmitMultipleExpandedEmptyNoChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => true, + 'choices' => array(), + )); + + $form->submit(array()); + + $this->assertSame(array(), $form->getData()); + $this->assertTrue($form->isSynchronized()); + } + + public function testSubmitMultipleExpandedWithEmptyChild() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => true, + 'choices' => array( + 'Empty' => '', + 'Not Empty' => 1, + 'Not Empty 2' => 2, + ), + )); + + $form->submit(array('', '2')); + + $this->assertSame(array('', 2), $form->getData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertTrue($form[0]->getData()); + $this->assertFalse($form[1]->getData()); + $this->assertTrue($form[2]->getData()); + $this->assertSame('', $form[0]->getViewData()); + $this->assertNull($form[1]->getViewData()); + $this->assertSame('2', $form[2]->getViewData()); + } + + public function testSubmitMultipleExpandedObjectChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => true, + 'choices' => $this->objectChoices, + 'choice_label' => 'name', + 'choice_value' => 'id', + )); + + $form->submit(array('1', '2')); + + $this->assertSame(array($this->objectChoices[0], $this->objectChoices[1]), $form->getData()); + $this->assertTrue($form->isSynchronized()); + + $this->assertTrue($form[0]->getData()); + $this->assertTrue($form[1]->getData()); + $this->assertFalse($form[2]->getData()); + $this->assertFalse($form[3]->getData()); + $this->assertFalse($form[4]->getData()); + $this->assertSame('1', $form[0]->getViewData()); + $this->assertSame('2', $form[1]->getViewData()); + $this->assertNull($form[2]->getViewData()); + $this->assertNull($form[3]->getViewData()); + $this->assertNull($form[4]->getViewData()); + } + + public function testSingleSelectedObjectChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', $this->objectChoices[3], array( + 'multiple' => false, + 'expanded' => false, + 'choices' => $this->objectChoices, + 'choice_label' => 'name', + 'choice_value' => 'id', + )); + + $view = $form->createView(); + $selectedChecker = $view->vars['is_selected']; + + $this->assertTrue($selectedChecker($view->vars['choices'][3]->value, $view->vars['value'])); + $this->assertFalse($selectedChecker($view->vars['choices'][1]->value, $view->vars['value'])); + } + + public function testMultipleSelectedObjectChoices() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', array($this->objectChoices[3]), array( + 'multiple' => true, + 'expanded' => false, + 'choices' => $this->objectChoices, + 'choice_label' => 'name', + 'choice_value' => 'id', + )); + + $view = $form->createView(); + $selectedChecker = $view->vars['is_selected']; + + $this->assertTrue($selectedChecker($view->vars['choices'][3]->value, $view->vars['value'])); + $this->assertFalse($selectedChecker($view->vars['choices'][1]->value, $view->vars['value'])); + } + + public function testPassExpandedToView() + { + $form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'expanded' => true, + 'choices' => $this->choices, + )); + $view = $form->createView(); + + $this->assertTrue($view->vars['expanded']); + } + + public function testAdjustFullNameForMultipleNonExpanded() + { + $form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\ChoiceType', null, array( + 'multiple' => true, + 'expanded' => false, + 'choices' => $this->choices, + )); + $view = $form->createView(); + + $this->assertSame('name[]', $view->vars['full_name']); + } +} \ No newline at end of file diff --git a/src/Symfony/Component/Form/Tests/Fixtures/ChoiceSubType.php b/src/Symfony/Component/Form/Tests/Fixtures/ChoiceSubType.php index dbd8843d8e7eb..764e1345f6f25 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/ChoiceSubType.php +++ b/src/Symfony/Component/Form/Tests/Fixtures/ChoiceSubType.php @@ -24,7 +24,7 @@ class ChoiceSubType extends AbstractType */ public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefaults(array('expanded' => true)); + $resolver->setDefaults(array('widget' => 'radio')); $resolver->setNormalizer('choices', function () { return array( 'attr1' => 'Attribute 1',