Skip to content

[Form] Fix #10658 Deprecate "read_only" option #10676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>" <?php if ($read_only): ?>readonly="readonly" <?php endif ?>
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>"
<?php if ($disabled): ?>disabled="disabled" <?php endif ?>
<?php if ($required): ?>required="required" <?php endif ?>
<?php foreach ($attr as $k => $v): ?>
Expand Down
12 changes: 6 additions & 6 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,20 @@ public function buildView(FormView $view, FormInterface $form, array $options)
parent::buildView($view, $form, $options);

$name = $form->getName();
$readOnly = $options['read_only'];

if ($view->parent) {
if ('' === $name) {
throw new LogicException('Form node with empty name can be used only as root form node.');
}

// Complex fields are read-only if they themselves or their parents are.
if (!$readOnly) {
$readOnly = $view->parent->vars['read_only'];
if (!$view->vars['attr']['readonly'] && (isset($view->parent->vars['attr']['readonly']) && false !== $view->parent->vars['attr']['readonly'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think view->vars['attr']['readonly'] is ensured to be set.

$view->vars['attr']['readonly'] = true;
}
}

$view->vars = array_replace($view->vars, array(
'read_only' => $readOnly,
'read_only' => $view->vars['attr']['readonly'],
'errors' => $form->getErrors(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to keep the view variable to maintain bc.

'valid' => $form->isSubmitted() ? $form->isValid() : true,
'value' => $form->getViewData(),
Expand Down Expand Up @@ -170,7 +169,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" option
// Add these values to the "attr" option instead
$defaultAttr = function (Options $options) {
$attributes = array();
Expand All @@ -182,6 +181,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
if (null !== $options['pattern']) {
$attributes['pattern'] = $options['pattern'];
}
$attributes['readonly'] = $options['read_only'];

return $attributes;
};
Expand All @@ -191,7 +191,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'empty_data' => $emptyData,
'trim' => true,
'required' => true,
'read_only' => false,
'read_only' => false, // Deprecated use attr['readonly'] instead
'max_length' => null,
'pattern' => null,
'property_path' => null,
Expand Down
20 changes: 17 additions & 3 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1291,12 +1291,27 @@ public function testHidden()
);
}

public function testReadOnly()
public function testReadOnlyBc()
{
$form = $this->factory->createNamed('name', 'text', null, array(
'read_only' => true,
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/input
[@type="text"]
[@name="name"]
[@readonly="readonly"]
'
);
}

public function testReadOnly()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please duplicate this test into a testReadOnlyBc that uses the old "read_only" option.

{
$form = $this->factory->createNamed('name', 'text', null, array(
'attr' => array('readonly' => true),
));

$this->assertWidgetMatchesXpath($form->createView(), array(),
'/input
[@type="text"]
Expand Down Expand Up @@ -1899,8 +1914,7 @@ 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+', 'class' => 'foobar', 'data-foo' => 'bar', 'readonly' => true),
));

$html = $this->renderWidget($form->createView());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Tests\Fixtures\Author;
use Symfony\Component\Form\Tests\Fixtures\FixedDataTransformer;
Expand Down Expand Up @@ -102,22 +101,22 @@ public function testSubmittedDataIsNotTrimmedBeforeTransformingIfNoTrimming()

public function testNonReadOnlyFormWithReadOnlyParentIsReadOnly()
{
$view = $this->factory->createNamedBuilder('parent', 'form', null, array('read_only' => true))
$view = $this->factory->createNamedBuilder('parent', 'form', null, array('attr' => array('readonly' => 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['read_only']);
$this->assertTrue($view['child']->vars['attr']['readonly']);
}

public function testNonReadOnlyFormWithNonReadOnlyParentIsNotReadOnly()
Expand All @@ -127,7 +126,7 @@ public function testNonReadOnlyFormWithNonReadOnlyParentIsNotReadOnly()
->getForm()
->createView();

$this->assertFalse($view['child']->vars['read_only']);
$this->assertFalse($view['child']->vars['attr']['readonly']);
}

public function testPassMaxLengthToView()
Expand Down