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

Commit 9524396

Browse filesBrowse files
committed
bug #25236 [Form][TwigBridge] Fix collision between view properties and form fields (yceruto)
This PR was merged into the 2.7 branch. Discussion ---------- [Form][TwigBridge] Fix collision between view properties and form fields | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #18882 | License | MIT | Doc PR | TODO This introduce a new Twig test function `rootform` that guarantee the right access to the `parent` property of the form view. The rest of the properties (`vars` and `children`) are not used at least inside Symfony repo. I've chosen this solution because it doesn't [affect the design of the form view class/interface](https://github.com/symfony/symfony/pull/19492/files#diff-f60b55ea46e40b9c4475a1bd361f6940R168) and because [the problem happen only on Twig](https://github.com/twigphp/Twig/blob/fd98722d15996561f598f9181dbcef8432e9ff85/lib/Twig/Extension/Core.php#L1439-L1447). More details about the problem here: * #24892 * #19492 * #23649 (comment) _if this is approved_ we should update also: * [`foundation_5_layout.html.twig`](https://github.com/symfony/symfony/blob/336600857b8bb47d5a7ba3d1924a0e7a3e2af7a8/src/Symfony/Bridge/Twig/Resources/views/Form/foundation_5_layout.html.twig#L321-L326) in `3.3` (done in #25305) * [`bootstrap_4_layout.html.twig`](https://github.com/symfony/symfony/blob/76d356f36a692dd8ad796de363484c97d6731d1f/src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_4_layout.html.twig#L176) in `3.4` (done in #25306) Commits ------- 8505894 Fix collision between view properties and form fields
2 parents 6eedbb5 + 8505894 commit 9524396
Copy full SHA for 9524396

File tree

Expand file treeCollapse file tree

5 files changed

+28
-5
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+28
-5
lines changed

‎src/Symfony/Bridge/Twig/Extension/FormExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Extension/FormExtension.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
1515
use Symfony\Bridge\Twig\Form\TwigRendererInterface;
1616
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
17+
use Symfony\Component\Form\FormView;
1718
use Twig\Environment;
1819
use Twig\Extension\AbstractExtension;
1920
use Twig\Extension\InitRuntimeInterface;
@@ -97,6 +98,7 @@ public function getTests()
9798
{
9899
return array(
99100
new TwigTest('selectedchoice', array($this, 'isSelectedChoice')),
101+
new TwigTest('rootform', array($this, 'isRootForm')),
100102
);
101103
}
102104

@@ -156,6 +158,11 @@ public function isSelectedChoice(ChoiceView $choice, $selectedValue)
156158
return $choice->value === $selectedValue;
157159
}
158160

161+
public function isRootForm(FormView $formView)
162+
{
163+
return null === $formView->parent;
164+
}
165+
159166
/**
160167
* {@inheritdoc}
161168
*/

‎src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/bootstrap_3_layout.html.twig
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,12 @@
238238

239239
{% block form_errors -%}
240240
{% if errors|length > 0 -%}
241-
{% if form.parent %}<span class="help-block">{% else %}<div class="alert alert-danger">{% endif %}
241+
{% if form is not rootform %}<span class="help-block">{% else %}<div class="alert alert-danger">{% endif %}
242242
<ul class="list-unstyled">
243243
{%- for error in errors -%}
244244
<li><span class="glyphicon glyphicon-exclamation-sign"></span> {{ error.message }}</li>
245245
{%- endfor -%}
246246
</ul>
247-
{% if form.parent %}</span>{% else %}</div>{% endif %}
247+
{% if form is not rootform %}</span>{% else %}</div>{% endif %}
248248
{%- endif %}
249249
{%- endblock form_errors %}

‎src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
{%- block form_widget_compound -%}
1717
<div {{ block('widget_container_attributes') }}>
18-
{%- if form.parent is empty -%}
18+
{%- if form is rootform -%}
1919
{{ form_errors(form) }}
2020
{%- endif -%}
2121
{{- block('form_rows') -}}
@@ -303,7 +303,7 @@
303303
{% endif %}
304304
{%- endfor %}
305305

306-
{% if not form.methodRendered and form.parent is null %}
306+
{% if not form.methodRendered and form is rootform %}
307307
{%- do form.setMethodRendered() -%}
308308
{% set method = method|upper %}
309309
{%- if method in ["GET", "POST"] -%}

‎src/Symfony/Bridge/Twig/Resources/views/Form/form_table_layout.html.twig

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Resources/views/Form/form_table_layout.html.twig
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
{%- block form_widget_compound -%}
3333
<table {{ block('widget_container_attributes') }}>
34-
{%- if form.parent is empty and errors|length > 0 -%}
34+
{%- if form is rootform and errors|length > 0 -%}
3535
<tr>
3636
<td colspan="2">
3737
{{- form_errors(form) -}}

‎src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionDivLayoutTest.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ public function testIsChoiceSelected($expected, $choice, $value)
146146
$this->assertSame($expected, $this->extension->isSelectedChoice($choice, $value));
147147
}
148148

149+
public function isRootFormProvider()
150+
{
151+
return array(
152+
array(true, new FormView()),
153+
array(false, new FormView(new FormView())),
154+
);
155+
}
156+
157+
/**
158+
* @dataProvider isRootFormProvider
159+
*/
160+
public function testIsRootForm($expected, FormView $formView)
161+
{
162+
$this->assertSame($expected, $this->extension->isRootForm($formView));
163+
}
164+
149165
protected function renderForm(FormView $view, array $vars = array())
150166
{
151167
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.