Skip to content

Commit dbb099d

Browse files
committed
feature #16029 [FrameworkBundle][TwigBridge] do not render empty form action attributes (xabbuh)
This PR was merged into the 2.8 branch. Discussion ---------- [FrameworkBundle][TwigBridge] do not render empty form action attributes | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13852, #15995 | License | MIT | Doc PR | Commits ------- 1307043 do not render empty form action attributes
2 parents 62a0ecd + 1307043 commit dbb099d

File tree

7 files changed

+62
-2
lines changed

7 files changed

+62
-2
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@
274274
{%- else -%}
275275
{% set form_method = "POST" %}
276276
{%- endif -%}
277-
<form name="{{ name }}" method="{{ form_method|lower }}" action="{{ action }}"{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}{% if multipart %} enctype="multipart/form-data"{% endif %}>
277+
<form name="{{ name }}" method="{{ form_method|lower }}"{% if action %} action="{{ action }}"{% endif %}{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}{% if multipart %} enctype="multipart/form-data"{% endif %}>
278278
{%- if form_method != method -%}
279279
<input type="hidden" name="_method" value="{{ method }}" />
280280
{%- endif -%}

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionBootstrap3LayoutTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ protected function tearDown()
6262
$this->extension = null;
6363
}
6464

65+
public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
66+
{
67+
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
68+
'method' => 'get',
69+
'action' => '',
70+
));
71+
72+
$html = $this->renderStart($form->createView());
73+
74+
$this->assertSame('<form name="form" method="get">', $html);
75+
}
76+
6577
protected function renderForm(FormView $view, array $vars = array())
6678
{
6779
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);

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

+12
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ public function testIsChoiceSelected($expected, $choice, $value)
141141
$this->assertSame($expected, $this->extension->isSelectedChoice($choice, $value));
142142
}
143143

144+
public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
145+
{
146+
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
147+
'method' => 'get',
148+
'action' => '',
149+
));
150+
151+
$html = $this->renderStart($form->createView());
152+
153+
$this->assertSame('<form name="form" method="get">', $html);
154+
}
155+
144156
protected function renderForm(FormView $view, array $vars = array())
145157
{
146158
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);

src/Symfony/Bridge/Twig/Tests/Extension/FormExtensionTableLayoutTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ protected function tearDown()
6363
$this->extension = null;
6464
}
6565

66+
public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
67+
{
68+
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
69+
'method' => 'get',
70+
'action' => '',
71+
));
72+
73+
$html = $this->renderStart($form->createView());
74+
75+
$this->assertSame('<form name="form" method="get">', $html);
76+
}
77+
6678
protected function renderForm(FormView $view, array $vars = array())
6779
{
6880
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php $method = strtoupper($method) ?>
22
<?php $form_method = $method === 'GET' || $method === 'POST' ? $method : 'POST' ?>
3-
<form name="<?php echo $name ?>" method="<?php echo strtolower($form_method) ?>" action="<?php echo $action ?>"<?php foreach ($attr as $k => $v) { printf(' %s="%s"', $view->escape($k), $view->escape($v)); } ?><?php if ($multipart): ?> enctype="multipart/form-data"<?php endif ?>>
3+
<form name="<?php echo $name ?>" method="<?php echo strtolower($form_method) ?>"<?php if ($action): ?> action="<?php echo $action ?>"<?php endif ?><?php foreach ($attr as $k => $v) { printf(' %s="%s"', $view->escape($k), $view->escape($v)); } ?><?php if ($multipart): ?> enctype="multipart/form-data"<?php endif ?>>
44
<?php if ($form_method !== $method): ?>
55
<input type="hidden" name="_method" value="<?php echo $method ?>" />
66
<?php endif ?>

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ protected function tearDown()
6161
parent::tearDown();
6262
}
6363

64+
public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
65+
{
66+
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
67+
'method' => 'get',
68+
'action' => '',
69+
));
70+
71+
$html = $this->renderStart($form->createView());
72+
73+
$this->assertSame('<form name="form" method="get">', $html);
74+
}
75+
6476
protected function renderForm(FormView $view, array $vars = array())
6577
{
6678
return (string) $this->engine->get('form')->form($view, $vars);

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableLayoutTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ class FormHelperTableLayoutTest extends AbstractTableLayoutTest
3131
'choice_attr',
3232
);
3333

34+
public function testStartTagHasNoActionAttributeWhenActionIsEmpty()
35+
{
36+
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, array(
37+
'method' => 'get',
38+
'action' => '',
39+
));
40+
41+
$html = $this->renderStart($form->createView());
42+
43+
$this->assertSame('<form name="form" method="get">', $html);
44+
}
45+
3446
protected function getExtensions()
3547
{
3648
// should be moved to the Form component once absolute file paths are supported

0 commit comments

Comments
 (0)