Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Form] deprecate read_only option #14403

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 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[Form] adjust tests for deprecated read_only option
  • Loading branch information
Tobion committed Apr 19, 2015
commit 83db940e3d2fcd1526de137cdd2cca14da4ff072
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
}

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

Choose a reason for hiding this comment

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

@mnapoli Ok so to avoid the BC break, we just need to add a line here with:

$view->vars['read_only'] = true; // BC`

Wrong this has to be done in the form theme.

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,10 @@ public function testHidden()
);
}

public function testReadOnly()
/**
* @group legacy
*/
public function testLegacyReadOnly()
{
$form = $this->factory->createNamed('name', 'text', null, array(
'read_only' => true,
Expand Down Expand Up @@ -1907,14 +1910,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('readonly' => true, 'maxlength' => 10, 'pattern' => '\d+', '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 form-control" data-foo="bar" value="value" />', $html);
$this->assertSame('<input type="text" id="text" name="text" disabled="disabled" required="required" readonly="readonly" maxlength="10" pattern="\d+" class="foobar form-control" data-foo="bar" value="value" />', $html);
}

public function testWidgetAttributeNameRepeatedIfTrue()
Expand Down
10 changes: 6 additions & 4 deletions 10 src/Symfony/Component/Form/Tests/AbstractLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,10 @@ public function testHidden()
);
}

public function testReadOnly()
/**
* @group legacy
*/
public function testLegacyReadOnly()
{
$form = $this->factory->createNamed('name', 'text', null, array(
'read_only' => true,
Expand Down Expand Up @@ -2119,14 +2122,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('readonly' => true, 'maxlength' => 10, 'pattern' => '\d+', '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" readonly="readonly" maxlength="10" pattern="\d+" class="foobar" data-foo="bar" value="value" />', $html);
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 understand, this doesn't preserve BC here? The test was changed to support the new way (through attributes), but the old way is not tested anymore.

This created a BC break for us when migrating from 2.7 to 2.8. We have to switch from the read_only variable to the attr key.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mnapoli what exactly is breaking except the position in the string ? One would just expect to get the attribute here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Before (2.7) passing read_only as a variable was working, after (2.8) it isn't. We had to put readonly in the attr array instead.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here is the change we had to do:

 {% block _form_category_name_widget %}
-    {% set read_only = true %}
     {% set attr = {
         'data-target-show': '.choose-categories',
+        'readonly': true,
     } %}
     {{ block('form_widget_simple') }}
{% endblock %}

}

public function testWidgetAttributeNameRepeatedIfTrue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ public function testSubmittedDataIsNotTrimmedBeforeTransformingIfNoTrimming()
$this->assertEquals('reverse[ a ]', $form->getData());
}

public function testNonReadOnlyFormWithReadOnlyParentIsReadOnly()
/**
* @group legacy
*/
public function testLegacyNonReadOnlyFormWithReadOnlyParentIsReadOnly()
{
$view = $this->factory->createNamedBuilder('parent', 'form', null, array('read_only' => true))
->add('child', 'form')
Expand All @@ -109,7 +112,20 @@ public function testNonReadOnlyFormWithReadOnlyParentIsReadOnly()
$this->assertTrue($view['child']->vars['read_only']);
}

public function testReadOnlyFormWithNonReadOnlyParentIsReadOnly()
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']);
}

/**
* @group legacy
*/
public function testLegacyReadOnlyFormWithNonReadOnlyParentIsReadOnly()
{
$view = $this->factory->createNamedBuilder('parent', 'form')
->add('child', 'form', array('read_only' => true))
Expand All @@ -119,7 +135,20 @@ public function testReadOnlyFormWithNonReadOnlyParentIsReadOnly()
$this->assertTrue($view['child']->vars['read_only']);
}

public function testNonReadOnlyFormWithNonReadOnlyParentIsNotReadOnly()
public function testReadOnlyFormWithNonReadOnlyParentIsReadOnly()
{
$view = $this->factory->createNamedBuilder('parent', 'form')
->add('child', 'form', array('attr' => array('readonly' => true)))
->getForm()
->createView();

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

/**
* @group legacy
*/
public function testLegacyNonReadOnlyFormWithNonReadOnlyParentIsNotReadOnly()
{
$view = $this->factory->createNamedBuilder('parent', 'form')
->add('child', 'form')
Expand All @@ -129,6 +158,16 @@ public function testNonReadOnlyFormWithNonReadOnlyParentIsNotReadOnly()
$this->assertFalse($view['child']->vars['read_only']);
}

public function testNonReadOnlyFormWithNonReadOnlyParentIsNotReadOnly()
{
$view = $this->factory->createNamedBuilder('parent', 'form')
->add('child', 'form')
->getForm()
->createView();

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

public function testPassMaxLengthToView()
{
$form = $this->factory->create('form', null, array('attr' => array('maxlength' => 10)));
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.