From 705beceff18710bd10bbcfd76c926d52abe5df10 Mon Sep 17 00:00:00 2001 From: Bill Hance Date: Wed, 30 Apr 2014 23:30:24 -0700 Subject: [PATCH 1/2] [Form] Deprecated read_only option --- .../views/Form/form_div_layout.html.twig | 1 - .../views/Form/widget_attributes.html.php | 2 +- .../Form/Extension/Core/Type/FormType.php | 12 +++++--- .../Form/Tests/AbstractLayoutTest.php | 7 ++--- .../Extension/Core/Type/FormTypeTest.php | 28 ++++++++++++++++--- 5 files changed, 36 insertions(+), 14 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 211eee5dd784f..dfa4b1dd1a9f5 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 @@ -374,7 +374,6 @@ {% block widget_attributes %} {% spaceless %} id="{{ id }}" name="{{ full_name }}" - {%- if read_only %} readonly="readonly"{% endif -%} {%- if disabled %} disabled="disabled"{% endif -%} {%- if required %} required="required"{% endif -%} {%- for attrname, attrvalue in attr -%} diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php index c2260477317e5..ff38706573e62 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/widget_attributes.html.php @@ -1,4 +1,4 @@ -id="escape($id) ?>" name="escape($full_name) ?>" readonly="readonly" +id="escape($id) ?>" name="escape($full_name) ?>" disabled="disabled" required="required" $v): ?> diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php index eafedfc5bbc61..44797a2768787 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php @@ -72,7 +72,7 @@ public function buildView(FormView $view, FormInterface $form, array $options) parent::buildView($view, $form, $options); $name = $form->getName(); - $readOnly = $options['read_only']; + $readOnly = isset($options['attr']['readonly']) ? $options['attr']['readonly'] : false; if ($view->parent) { if ('' === $name) { @@ -81,12 +81,12 @@ public function buildView(FormView $view, FormInterface $form, array $options) // Complex fields are read-only if they themselves or their parents are. if (!$readOnly) { - $readOnly = $view->parent->vars['read_only']; + $readOnly = isset($view->parent->vars['attr']['readonly']) ? $view->parent->vars['attr']['readonly'] : false; } } $view->vars = array_replace($view->vars, array( - 'read_only' => $readOnly, + 'read_only' => $readOnly, // Deprecated 'errors' => $form->getErrors(), 'valid' => $form->isSubmitted() ? $form->isValid() : true, 'value' => $form->getViewData(), @@ -170,7 +170,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver) 'data', )); - // BC clause for the "max_length" and "pattern" option + // BC clause for the "max_length", "pattern" and "read_only" options // Add these values to the "attr" option instead $defaultAttr = function (Options $options) { $attributes = array(); @@ -183,6 +183,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver) $attributes['pattern'] = $options['pattern']; } + if (false !== $options['read_only']) { + $attributes['readonly'] = $options['read_only']; + } + return $attributes; }; diff --git a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php index 532dc41be75f5..cf32f73d55f49 100644 --- a/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractLayoutTest.php @@ -1294,7 +1294,7 @@ public function testHidden() public function testReadOnly() { $form = $this->factory->createNamed('name', 'text', null, array( - 'read_only' => true, + 'attr' => array('readonly' => true), )); $this->assertWidgetMatchesXpath($form->createView(), array(), @@ -1899,14 +1899,13 @@ public function testWidgetAttributes() $form = $this->factory->createNamed('text', 'text', 'value', array( 'required' => true, 'disabled' => true, - 'read_only' => true, - 'attr' => array('maxlength' => 10, 'pattern' => '\d+', 'class' => 'foobar', 'data-foo' => 'bar'), + 'attr' => array('maxlength' => 10, 'pattern' => '\d+', 'readonly' => true, 'class' => 'foobar', 'data-foo' => 'bar'), )); $html = $this->renderWidget($form->createView()); // compare plain HTML to check the whitespace - $this->assertSame('', $html); + $this->assertSame('', $html); } public function testWidgetAttributeNameRepeatedIfTrue() diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php index a8465dc99083a..0a8415fadd469 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php @@ -101,23 +101,43 @@ public function testSubmittedDataIsNotTrimmedBeforeTransformingIfNoTrimming() } public function testNonReadOnlyFormWithReadOnlyParentIsReadOnly() + { + $view = $this->factory->createNamedBuilder('parent', 'form', null, array('attr' => array('readonly' => true))) + ->add('child', 'form') + ->getForm() + ->createView(); + + $this->assertTrue($view['child']->vars['attr']['readonly']); + } + + public function testNonReadOnlyFormWithReadOnlyParentBCIsReadOnly() { $view = $this->factory->createNamedBuilder('parent', 'form', null, array('read_only' => true)) ->add('child', 'form') ->getForm() ->createView(); - $this->assertTrue($view['child']->vars['read_only']); + $this->assertTrue($view['child']->vars['attr']['readonly']); } public function testReadOnlyFormWithNonReadOnlyParentIsReadOnly() { $view = $this->factory->createNamedBuilder('parent', 'form') - ->add('child', 'form', array('read_only' => true)) + ->add('child', 'form', array('attr' => array('readonly' => true))) + ->getForm() + ->createView(); + + $this->assertTrue($view['child']->vars['attr']['readonly']); + } + + public function testReadOnlyFormWithNonReadOnlyParentBCIsReadOnly() + { + $view = $this->factory->createNamedBuilder('parent', 'form') + ->add('child', 'form', array('readonly' => true)) ->getForm() ->createView(); - $this->assertTrue($view['child']->vars['read_only']); + $this->assertTrue($view['child']->vars['attr']['readonly']); } public function testNonReadOnlyFormWithNonReadOnlyParentIsNotReadOnly() @@ -127,7 +147,7 @@ public function testNonReadOnlyFormWithNonReadOnlyParentIsNotReadOnly() ->getForm() ->createView(); - $this->assertFalse($view['child']->vars['read_only']); + $this->assertFalse($view['child']->vars['attr']['readonly']); } public function testPassMaxLengthToView() From cb5db1dcf0f1e61295945b2ebfd95037893e87d6 Mon Sep 17 00:00:00 2001 From: Bill Hance Date: Wed, 30 Apr 2014 23:39:46 -0700 Subject: [PATCH 2/2] Added deprecation of read_only option in CHANGELOG --- src/Symfony/Component/Form/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 2999e1ffcff97..f0b3295954288 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -4,7 +4,7 @@ CHANGELOG 2.5.0 ------ - * deprecated options "max_length" and "pattern" in favor of putting these values in "attr" option + * deprecated options "max_length", "pattern" and "read_only" in favor of putting these values in "attr" option * added an option for multiple files upload * form errors now reference their cause (constraint violation, exception, ...) * form errors now remember which form they were originally added to