10000 [Form] Deprecate `searchAndRenderBlock` returning empty string by ostrolucky · Pull Request #27247 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Deprecate searchAndRenderBlock returning empty string #27247

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

Merged
Merged
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
19 changes: 19 additions & 0 deletions UPGRADE-4.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ Cache

* Deprecated `CacheItem::getPreviousTags()`, use `CacheItem::getMetadata()` instead.

Form
----

* Deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered.
Instead of expecting such calls to return empty strings, check if the field has already been rendered.

Before:
```twig
{% for field in fieldsWithPotentialDuplicates %}
{{ form_widget(field) }}
{% endfor %}
```

After:
```twig
{% for field in fieldsWithPotentialDuplicates if not field.rendered %}
{{ form_widget(field) }}
{% endfor %}
```
Security
--------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@
{# Support #}

{%- block form_rows -%}
{% for child in form %}
{% for child in form if not child.rendered %}
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we really do this change? Better get the deprecation and then the exception if you're trying to render the same row twice?

Copy link
Member

Choose a reason for hiding this comment

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

isn't this required to support partial form auto-rendering?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's already supposed to be handled in form_rest:

{%- block form_rest -%}
{% for child in form -%}
{% if not child.rendered %}
{{- form_row(child) -}}
{% endif %}
{%- endfor -%}

Copy link
Contributor Author
@ostrolucky ostrolucky May 15, 2018

Choose a reason for hiding this comment

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

This change is necessary for tests to pass, which means not doing this would be BC break

Copy link
Contributor

Choose a reason for hiding this comment

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

Which test? If the test is failing by triggering the deprecation, that's expected and should by marked as legacy, right?

Copy link
Contributor Author
@ostrolucky ostrolucky May 15, 2018

Choose a reason for hiding this comment

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

Ok yes, it's just deprecation. It was triggered by FormExtensionDivLayoutTest, inherited testcase testRestWithChildrenForms.

Negative of just marking tests as legacy is that it triggers unresolvable deprecation notices in user land. There should be some effort in avoiding it, not just mark test as legacy as first thing IMHO.

I might have found another way though

edit: here is a chain of calls which trigger deprecation:

form_rest -> form_row -> form_widget_compound -> form_rows

{{- form_row(child) -}}
{% endfor %}
{%- endblock form_rows -%}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<?php foreach ($form as $child) : ?>
<?php echo $view['form']->row($child) ?>
<?php if (!$child->isRendered()): ?>
<?php echo $view['form']->row($child) ?>
<?php endif; ?>
<?php endforeach; ?>
5 changes: 5 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

* deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered
< 8000 /td>
4.1.0
-----

Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/FormRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public function searchAndRenderBlock(FormView $view, $blockNameSuffix, array $va
$renderOnlyOnce = 'row' === $blockNameSuffix || 'widget' === $blockNameSuffix;

if ($renderOnlyOnce && $view->isRendered()) {
// This is not allowed, because it would result in rendering same IDs multiple times, which is not valid.
@trigger_error(sprintf('You are calling "form_%s" for field "%s" which has already been rendered before, trying to render fields which were already rendered is deprecated since Symfony 4.2 and will throw an exception in 5.0.', $blockNameSuffix, $view->vars['name']), E_USER_DEPRECATED);
// throw new BadMethodCallException(sprintf('Field "%s" has already been rendered. Save result of previous render call to variable and output that instead.', $view->vars['name']));
return '';
}

Expand Down
0