Skip to content

Commit fcd941c

Browse files
committed
merged branch bschussek/issue5493 (PR #6522)
This PR was merged into the master branch. Discussion ---------- [2.3] [Form] Implemented form processors Bug fix: no Feature addition: yes Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: partially #5493 Todo: - License of the code: MIT Documentation PR: symfony/symfony-docs#2092 Commits ------- 11fee06 [TwigBridge] Removed duplicate entries from the CHANGELOG 68f360c [Form] Moved upgrade nodes to UPGRADE-3.0 01b71a4 [Form] Removed trigger_error() for deprecations as of 3.0 81f8c67 [Form] Implemented form processors 0ea75db [Form] Improved FormRenderer::renderBlock() to be usable outside of form blocks
2 parents e51c560 + 11fee06 commit fcd941c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1967
-54
lines changed

UPGRADE-2.3.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
UPGRADE FROM 2.2 to 2.3
1+
UPGRADE FROM 2.2 to 2.3
22
=======================
33

44
### Form

UPGRADE-3.0.md

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,97 @@ UPGRADE FROM 2.x to 3.0
1818
`DebugClassLoader`. The difference is that the constructor now takes a
1919
loader to wrap.
2020

21+
### Form
22+
23+
* Passing a `Symfony\Component\HttpFoundation\Request` instance to
24+
`FormInterface::bind()` was disabled. You should use
25+
`FormInterface::process()` instead.
26+
27+
Before:
28+
29+
```
30+
if ('POST' === $request->getMethod()) {
31+
$form->bind($request);
32+
33+
if ($form->isValid()) {
34+
// ...
35+
}
36+
}
37+
```
38+
39+
After:
40+
41+
```
42+
if ($form->process($request)->isValid()) {
43+
// ...
44+
}
45+
```
46+
47+
If you want to test whether the form was submitted separately, you can use
48+
the method `isBound()`:
49+
50+
```
51+
if ($form->process($request)->isBound()) {
52+
// ...
53+
54+
if ($form->isValid()) {
55+
// ...
56+
}
57+
}
58+
```
59+
60+
### FrameworkBundle
61+
62+
* The `enctype` method of the `form` helper was removed. You should use the
63+
new method `start` instead.
64+
65+
Before:
66+
67+
```
68+
<form method="post" action="http://example.com" <?php echo $view['form']->enctype($form) ?>>
69+
...
70+
</form>
71+
```
72+
73+
After:
74+
75+
```
76+
<?php echo $view['form']->start($form) ?>
77+
...
78+
<?php echo $view['form']->end($form) ?>
79+
```
80+
81+
The method and action of the form default to "POST" and the current
82+
document. If you want to change these values, you can set them explicitly in
83+
the controller.
84+
85+
Alternative 1:
86+
87+
```
88+
$form = $this->createForm('my_form', $formData, array(
89+
'method' => 'PUT',
90+
'action' => $this->generateUrl('target_route'),
91+
));
92+
```
93+
94+
Alternative 2:
95+
96+
```
97+
$form = $this->createFormBuilder($formData)
98+
// ...
99+
->setMethod('PUT')
100+
->setAction($this->generateUrl('target_route'))
101+
->getForm();
102+
```
103+
104+
It is also possible to override the method and the action in the template:
105+
106+
```
107+
<?php echo $view['form']->start($form, array('method' => 'GET', 'action' => 'http://example.com')) ?>
108+
...
109+
<?php echo $view['form']->end($form) ?>
110+
```
111+
21112
### HttpKernel
22113

23114
* The `Symfony\Component\HttpKernel\Log\LoggerInterface` has been removed in
@@ -98,6 +189,56 @@ UPGRADE FROM 2.x to 3.0
98189

99190
* The `render` tag is deprecated in favor of the `render` function.
100191

192+
* The `form_enctype` helper was removed. You should use the new `form_start`
193+
function instead.
194+
195+
Before:
196+
197+
```
198+
<form method="post" action="http://example.com" {{ form_enctype(form) }}>
199+
...
200+
</form>
201+
```
202+
203+
After:
204+
205+
```
206+
{{ form_start(form) }}
207+
...
208+
{{ form_end(form) }}
209+
```
210+
211+
The method and action of the form default to "POST" and the current
212+
document. If you want to change these values, you can set them explicitly in
213+
the controller.
214+
215+
Alternative 1:
216+
217+
```
218+
$form = $this->createForm('my_form', $formData, array(
219+
'method' => 'PUT',
220+
'action' => $this->generateUrl('target_route'),
221+
));
222+
```
223+
224+
Alternative 2:
225+
226+
```
227+
$form = $this->createFormBuilder($formData)
228+
// ...
229+
->setMethod('PUT')
230+
->setAction($this->generateUrl('target_route'))
231+
->getForm();
232+
```
233+
234+
It is also possible to override the method and the action in the template:
235+
236+
```
237+
{{ form_start(form, {'method': 'GET', 'action': 'http://example.com'}) }}
238+
...
239+
{{ form_end(form) }}
240+
```
241+
101242
### Yaml
102243

103244
* The ability to pass file names to `Yaml::parse()` has been removed.

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.3.0
5+
-----
6+
7+
* added helpers form(), form_start() and form_end()
8+
* deprecated form_enctype() in favor of form_start()
9+
410
2.2.0
511
-----
612

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ public function getTokenParsers()
6161
public function getFunctions()
6262
{
6363
return array(
64-
'form_enctype' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
64+
'form_enctype' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\FormEnctypeNode', array('is_safe' => array('html'))),
6565
'form_widget' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
6666
'form_errors' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
6767
'form_label' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
6868
'form_row' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
6969
'form_rest' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
70+
'form_start' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\RenderBlockNode', array('is_safe' => array('html'))),
71+
'form_end' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\RenderBlockNode', array('is_safe' => array('html'))),
7072
'csrf_token' => new \Twig_Function_Method($this, 'renderer->renderCsrfToken'),
7173
);
7274
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Node;
13+
14+
/**
15+
* @author Bernhard Schussek <bschussek@gmail.com>
16+
*
17+
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Use
18+
* the helper "form_start()" instead.
19+
*/
20+
class FormEnctypeNode extends SearchAndRenderBlockNode
21+
{
22+
public function compile(\Twig_Compiler $compiler)
23+
{
24+
parent::compile($compiler);
25+
26+
$compiler->raw(";\n");
27+
28+
// Uncomment this as soon as the deprecation note should be shown
29+
// $compiler->write('trigger_error(\'The helper form_enctype(form) is deprecated since version 2.3 and will be removed in 3.0. Use form_start(form) instead.\', E_USER_DEPRECATED)');
30+
}
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Node;
13+
14+
/**
15+
* Compiles a call to {@link FormRendererInterface::renderBlock()}.
16+
*
17+
* The function name is used as block name. For example, if the function name
18+
* is "foo", the block "foo" will be rendered.
19+
*
20+
* @author Bernhard Schussek <bschussek@gmail.com>
21+
*/
22+
class RenderBlockNode extends \Twig_Node_Expression_Function
23+
{
24+
public function compile(\Twig_Compiler $compiler)
25+
{
26+
$compiler->addDebugInfo($this);
27+
$arguments = iterator_to_array($this->getNode('arguments'));
28+
$compiler->write('$this->env->getExtension(\'form\')->renderer->renderBlock(');
29+
30+
if (isset($arguments[0])) {
31+
$compiler->subcompile($arguments[0]);
32+
$compiler->raw(', \'' . $this->getAttribute('name') . '\'');
33+
34+
if (isset($arguments[1])) {
35+
$compiler->raw(', ');
36+
$compiler->subcompile($arguments[1]);
37+
}
38+
}
39+
40+
$compiler->raw(')');
41+
}
42+
}

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,38 @@
298298

299299
{# Misc #}
300300

301+
{% block form %}
302+
{% spaceless %}
303+
{{ form_start(form) }}
304+
{{ form_widget(form) }}
305+
{{ form_end(form) }}
306+
{% endspaceless %}
307+
{% endblock form %}
308+
309+
{% block form_start %}
310+
{% spaceless %}
311+
{% set method = method|upper %}
312+
{% if method in ["GET", "POST"] %}
313+
{% set form_method = method %}
314+
{% else %}
315+
{% set form_method = "POST" %}
316+
{% endif %}
317+
<form method="{{ form_method|lower }}" action="{{ action }}"{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}{% if multipart %} enctype="multipart/form-data"{% endif %}>
318+
{% if form_method != method %}
319+
<input type="hidden" name="_method" value="{{ method }}" />
320+
{% endif %}
321+
{% endspaceless %}
322+
{% endblock form_start %}
323+
324+
{% block form_end %}
325+
{% spaceless %}
326+
{% if not render_rest is defined or render_rest %}
327+
{{ form_rest(form) }}
328+
{% endif %}
329+
</form>
330+
{% endspaceless %}
331+
{% endblock form_end %}
332+
301333
{% block form_enctype %}
302334
{% spaceless %}
303335
{% if multipart %}enctype="multipart/form-data"{% endif %}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ public function testIsChoiceSelected($expected, $choice, $value)
139139
$this->assertSame($expected, $this->extension->isSelectedChoice($choice, $value));
140140
}
141141

142+
protected function renderForm(FormView $view, array $vars = array())
143+
{
144+
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
145+
}
146+
142147
protected function renderEnctype(FormView $view)
143148
{
144149
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'enctype');
@@ -173,6 +178,16 @@ protected function renderRest(FormView $view, array $vars = array())
173178
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars);
174179
}
175180

181+
protected function renderStart(FormView $view, array $vars = array())
182+
{
183+
return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars);
184+
}
185+
186+
protected function renderEnd(FormView $view, array $vars = array())
187+
{
188+
return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars);
189+
}
190+
176191
protected function setTheme(FormView $view, array $themes)
177192
{
178193
$this->extension->renderer->setTheme($view, $themes);

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ protected function tearDown()
7575
$this->extension = null;
7676
}
7777

78+
protected function renderForm(FormView $view, array $vars = array())
79+
{
80+
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
81+
}
82+
7883
protected function renderEnctype(FormView $view)
7984
{
8085
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'enctype');
@@ -109,6 +114,16 @@ protected function renderRest(FormView $view, array $vars = array())
109114
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars);
110115
}
111116

117+
protected function renderStart(FormView $view, array $vars = array())
118+
{
119+
return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars);
120+
}
121+
122+
protected function renderEnd(FormView $view, array $vars = array())
123+
{
124+
return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars);
125+
}
126+
112127
protected function setTheme(FormView $view, array $themes)
113128
{
114129
$this->extension->renderer->setTheme($view, $themes);

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ CHANGELOG
1010
* added `TimedPhpEngine`
1111
* added `--clean` option the the `translation:update` command
1212
* added `http_method_override` option
13+
* added support for default templates per render tag
14+
* added FormHelper::form(), FormHelper::start() and FormHelper::end()
15+
* deprecated FormHelper::enctype() in favor of FormHelper::start()
1316

1417
2.2.0
1518
-----
@@ -27,10 +30,10 @@ CHANGELOG
2730
* replaced Symfony\Bundle\FrameworkBundle\Controller\TraceableControllerResolver by Symfony\Component\HttpKernel\Controller\TraceableControllerResolver
2831
* replaced Symfony\Component\HttpKernel\Debug\ContainerAwareTraceableEventDispatcher by Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher
2932
* added Client::enableProfiler()
30-
* A new parameter has been added to the DIC: `router.request_context.base_url`
33+
* a new parameter has been added to the DIC: `router.request_context.base_url`
3134
You can customize it for your functional tests or for generating urls with
3235
the right base url when your are in the cli context.
33-
* Added support for default templates per render tag
36+
* added support for default templates per render tag
3437

3538
2.1.0
3639
-----

0 commit comments

Comments
 (0)