-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Changes from all commits
7284680
052168f
83db940
40f4440
f0dfd2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>" <?php if ($read_only): ?>readonly="readonly" <?php endif ?> | ||
<?php if ($disabled): ?>disabled="disabled" <?php endif ?> | ||
<?php if ($required): ?>required="required" <?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): ?> | ||
<?php if (in_array($k, array('placeholder', 'title'), true)): ?> | ||
<?php printf('%s="%s" ', $view->escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?> | ||
<?php printf(' %s="%s"', $view->escape($k), $view->escape($view['translator']->trans($v, array(), $translation_domain))) ?> | ||
<?php elseif ($v === true): ?> | ||
<?php printf('%s="%s" ', $view->escape($k), $view->escape($k)) ?> | ||
<?php printf(' %s="%s"', $view->escape($k), $view->escape($k)) ?> | ||
<?php elseif ($v !== false): ?> | ||
<?php printf('%s="%s" ', $view->escape($k), $view->escape($v)) ?> | ||
<?php printf(' %s="%s"', $view->escape($k), $view->escape($v)) ?> | ||
<?php endif ?> | ||
<?php endforeach ?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
2.8.0 | ||
----- | ||
|
||
* deprecated option "read_only" in favor of "attr['readonly']" | ||
|
||
2.7.0 | ||
----- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,21 +71,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 (!isset($view->vars['attr']['readonly']) && isset($view->parent->vars['attr']['readonly']) && false !== $view->parent->vars['attr']['readonly']) { | ||
$view->vars['attr']['readonly'] = true; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mnapoli Ok $view->vars['read_only'] = true; // BC` Wrong this has to be done in the form theme. |
||
} | ||
|
||
$view->vars = array_replace($view->vars, array( | ||
'read_only' => $readOnly, | ||
'read_only' => isset($view->vars['attr']['readonly']) && false !== $view->vars['attr']['readonly'], // deprecated | ||
'errors' => $form->getErrors(), | ||
'valid' => $form->isSubmitted() ? $form->isValid() : true, | ||
'value' => $form->getViewData(), | ||
|
@@ -185,12 +184,31 @@ public function configureOptions(OptionsResolver $resolver) | |
return $attributes; | ||
}; | ||
|
||
// BC for "read_only" option | ||
$attrNormalizer = function (Options $options, array $attr) { | ||
if (!isset($attr['readonly']) && $options['read_only']) { | ||
$attr['readonly'] = true; | ||
} | ||
|
||
return $attr; | ||
}; | ||
|
||
$readOnlyNormalizer = function (Options $options, $readOnly) { | ||
if (null !== $readOnly) { | ||
trigger_error('The form option "read_only" is deprecated since version 2.8 and will be removed in 3.0. Use "attr[\'readonly\']" instead.', E_USER_DEPRECATED); | ||
|
||
return $readOnly; | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
$resolver->setDefaults(array( | ||
'data_class' => $dataClass, | ||
'empty_data' => $emptyData, | ||
'trim' => true, | ||
'required' => true, | ||
'read_only' => false, | ||
'read_only' => null, // deprecated | ||
'max_length' => null, | ||
'pattern' => null, | ||
'property_path' => null, | ||
|
@@ -209,6 +227,9 @@ public function configureOptions(OptionsResolver $resolver) | |
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', | ||
)); | ||
|
||
$resolver->setNormalizer('attr', $attrNormalizer); | ||
$resolver->setNormalizer('read_only', $readOnlyNormalizer); | ||
|
||
$resolver->setAllowedTypes('label_attr', 'array'); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before (2.7) passing There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mnapoli Could you please try to reverse the change on this line to confirm it fixes it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry I don't think I'll have time to test, but I believe the test that was changed in this pull request could be used to check? (i.e. revert the test and see if it fails/passes)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mnapoli see #18275