From 61d792eebdb0c718c19416cb20d8b35ba0ca5ecf Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Tue, 10 Apr 2012 17:47:40 +0200 Subject: [PATCH] [Form] Changed checkboxes in an expanded multiple-choice field to not include the choice index --- CHANGELOG-2.1.md | 2 ++ .../Form/Extension/Core/Type/ChoiceType.php | 20 ++++++++++++++++ .../Form/Extension/Core/Type/RadioType.php | 10 -------- .../Form/Tests/AbstractLayoutTest.php | 6 ++--- .../Extension/Core/Type/RadioTypeTest.php | 24 ------------------- 5 files changed, 25 insertions(+), 37 deletions(-) delete mode 100644 src/Symfony/Component/Form/Tests/Extension/Core/Type/RadioTypeTest.php diff --git a/CHANGELOG-2.1.md b/CHANGELOG-2.1.md index e535ccce20..3eda1cc61f 100644 --- a/CHANGELOG-2.1.md +++ b/CHANGELOG-2.1.md @@ -263,6 +263,8 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c in class Form now throw an exception if the form is already bound * fields of constrained classes without a NotBlank or NotNull constraint are set to not required now, as stated in the docs + * checkboxes of in an expanded multiple-choice field don't include the choice + in their name anymore. Their names terminate with "[]" now. ### HttpFoundation diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index f3309e930e..b79d85347a 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -143,6 +143,26 @@ class ChoiceType extends AbstractType } } + /** + * {@inheritdoc} + */ + public function buildViewBottomUp(FormView $view, FormInterface $form) + { + if ($view->get('expanded')) { + // Radio buttons should have the same name as the parent + $childName = $view->get('full_name'); + + // Checkboxes should append "[]" to allow multiple selection + if ($view->get('multiple')) { + $childName .= '[]'; + } + + foreach ($view->getChildren() as $childView) { + $childView->set('full_name', $childName); + } + } + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/Form/Extension/Core/Type/RadioType.php b/src/Symfony/Component/Form/Extension/Core/Type/RadioType.php index 2500e66c38..fef673e520 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/RadioType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/RadioType.php @@ -17,16 +17,6 @@ use Symfony\Component\Form\FormView; class RadioType extends AbstractType { - /** - * {@inheritdoc} - */ - public function buildView(FormView $view, FormInterface $form) - { - if ($view->hasParent()) { - $view->set('full_name', $view->getParent()->get('full_name')); - } - } - /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index f380a8372b..4b0361def6 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -711,11 +711,11 @@ abstract class AbstractLayoutTest extends \PHPUnit_Framework_TestCase $this->assertWidgetMatchesXpath($form->createView(), array(), '/div [ - ./input[@type="checkbox"][@name="name[0]"][@id="name_0"][@checked][not(@required)] + ./input[@type="checkbox"][@name="name[]"][@id="name_0"][@checked][not(@required)] /following-sibling::label[@for="name_0"][.="[trans]Choice&A[/trans]"] - /following-sibling::input[@type="checkbox"][@name="name[1]"][@id="name_1"][not(@checked)][not(@required)] + /following-sibling::input[@type="checkbox"][@name="name[]"][@id="name_1"][not(@checked)][not(@required)] /following-sibling::label[@for="name_1"][.="[trans]Choice&B[/trans]"] - /following-sibling::input[@type="checkbox"][@name="name[2]"][@id="name_2"][@checked][not(@required)] + /following-sibling::input[@type="checkbox"][@name="name[]"][@id="name_2"][@checked][not(@required)] /following-sibling::label[@for="name_2"][.="[trans]Choice&C[/trans]"] ] [count(./input)=3] diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/RadioTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/RadioTypeTest.php deleted file mode 100644 index 9681fdb66d..0000000000 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/RadioTypeTest.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * 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; - -class RadioTypeTest extends TypeTestCase -{ - public function testPassParentFullNameToView() - { - $parent = $this->factory->createNamed('field', 'parent'); - $parent->add($this->factory->createNamed('radio', 'child')); - $view = $parent->createView(); - - $this->assertEquals('parent', $view['child']->get('full_name')); - } -}