8000 [Form] Deprecated read_only option by billhance · Pull Request #10830 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Deprecated read_only option #10830

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 2 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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(),
Expand Down Expand Up @@ -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();
Expand All @@ -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;
};

Expand Down
7 changes: 3 additions & 4 deletions src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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('<input type="text" id="text" name="text" readonly="readonly" disabled="disabled" required="required" maxlength="10" pattern="\d+" class="foobar" data-foo="bar" value="value" />', $html);
$this->assertSame('<input type="text" id="text" name="text" disabled="disabled" required="required" maxlength="10" pattern="\d+" readonly="readonly" class="foobar" data-foo="bar" value="value" />', $html);
}

public function testWidgetAttributeNameRepeatedIfTrue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
0