From ef758d074622d5c61209e1120f6f7571d1912c8a Mon Sep 17 00:00:00 2001 From: Helmer Aaviksoo Date: Thu, 2 Feb 2012 13:51:41 +0200 Subject: [PATCH 1/2] [Form] Fixed read_only attribute for expanded fields --- .../Form/Extension/Core/Type/FieldType.php | 6 ++++- src/Symfony/Component/Form/FormBuilder.php | 2 +- .../Extension/Core/Type/FieldTypeTest.php | 27 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php b/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php index abbe5582085c..4dfdad64c71b 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php @@ -73,6 +73,7 @@ public function buildForm(FormBuilder $builder, array $options) public function buildView(FormView $view, FormInterface $form) { $name = $form->getName(); + $readOnly = $form->getAttribute('read_only'); if ($view->hasParent()) { if ('' === $name) { @@ -86,6 +87,9 @@ public function buildView(FormView $view, FormInterface $form) $id = $name; $fullName = $name; } + + // Complex fields are read-only if themselves or their parent is. + $readOnly = $readOnly || $view->getParent()->get('read_only'); } else { $id = $name; $fullName = $name; @@ -106,9 +110,9 @@ public function buildView(FormView $view, FormInterface $form) ->set('id', $id) ->set('name', $name) ->set('full_name', $fullName) + ->set('read_only', $readOnly) ->set('errors', $form->getErrors()) ->set('value', $form->getClientData()) - ->set('read_only', $form->getAttribute('read_only')) ->set('disabled', $form->isDisabled()) ->set('required', $form->isRequired()) ->set('max_length', $form->getAttribute('max_length')) diff --git a/src/Symfony/Component/Form/FormBuilder.php b/src/Symfony/Component/Form/FormBuilder.php index 92ee978311c6..388111c0c0f6 100644 --- a/src/Symfony/Component/Form/FormBuilder.php +++ b/src/Symfony/Component/Form/FormBuilder.php @@ -184,7 +184,7 @@ public function getData() } /** - * Set whether the form is disabled + * Set whether the form is disabled. * * @param Boolean $disabled Whether the form is disabled * diff --git a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php index 879d4fd35a88..963ef8578571 100644 --- a/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php +++ b/tests/Symfony/Tests/Component/Form/Extension/Core/Type/FieldTypeTest.php @@ -150,6 +150,33 @@ public function testPassIdAndNameToViewWithGrandParent() $this->assertEquals('parent[child][grand_child]', $view['child']['grand_child']->get('full_name')); } + public function testNonReadOnlyFieldWithReadOnlyParentBeingReadOnly() + { + $parent = $this->factory->createNamed('field', 'parent', null, array('read_only' => true)); + $child = $this->factory->createNamed('field', 'child'); + $view = $parent->add($child)->createView(); + + $this->assertTrue($view['child']->get('read_only')); + } + + public function testReadOnlyFieldWithNonReadOnlyParentBeingReadOnly() + { + $parent = $this->factory->createNamed('field', 'parent'); + $child = $this->factory->createNamed('field', 'child', null, array('read_only' => true)); + $view = $parent->add($child)->createView(); + + $this->assertTrue($view['child']->get('read_only')); + } + + public function testNonReadOnlyFieldWithNonReadOnlyParentBeingNonReadOnly() + { + $parent = $this->factory->createNamed('field', 'parent'); + $child = $this->factory->createNamed('field', 'child'); + $view = $parent->add($child)->createView(); + + $this->assertFalse($view['child']->get('read_only')); + } + public function testPassMaxLengthToView() { $form = $this->factory->create('field', null, array('max_length' => 10)); From d57116f5f95c95eeffb832b27396441ac4bfe7eb Mon Sep 17 00:00:00 2001 From: Helmer Aaviksoo Date: Thu, 2 Feb 2012 21:39:13 +0200 Subject: [PATCH 2/2] [Form] DRYed ChoiceType --- .../Form/Extension/Core/Type/ChoiceType.php | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index 574bf15aafb7..d828e21c4a8c 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -190,21 +190,23 @@ private function addSubFields(FormBuilder $builder, array $choiceViews, array $o if (is_array($choiceView)) { // Flatten groups $this->addSubFields($builder, $choiceView, $options); - } elseif ($options['multiple']) { - $builder->add((string) $i, 'checkbox', array( - 'value' => $choiceView->getValue(), - 'label' => $choiceView->getLabel(), - // The user can check 0 or more checkboxes. If required - // is true, he is required to check all of them. - 'required' => false, - 'translation_domain' => $options['translation_domain'], - )); } else { - $builder->add((string) $i, 'radio', array( + $choiceOpts = array( 'value' => $choiceView->getValue(), 'label' => $choiceView->getLabel(), 'translation_domain' => $options['translation_domain'], - )); + ); + + if ($options['multiple']) { + $choiceType = 'checkbox'; + // The user can check 0 or more checkboxes. If required + // is true, he is required to check all of them. + $choiceOpts['required'] = false; + } else { + $choiceType = 'radio'; + } + + $builder->add((string) $i, $choiceType, $choiceOpts); } } }