Skip to content

Commit 81f8c67

Browse files
committed
[Form] Implemented form processors
1 parent 0ea75db commit 81f8c67

40 files changed

+1809
-47
lines changed

src/Symfony/Bridge/Twig/CHANGELOG.md

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

4+
2.3.0
5+
-----
6+
7+
* [BC BREAK] restricted the `render` tag to only accept URIs as reference (the signature changed)
8+
* added a render function to render a request
9+
* the `app` global variable is now injected even when using the twig service directly.
10+
* added helpers form(), form_start() and form_end()
11+
* deprecated form_enctype() in favor of form_start()
12+
413
2.2.0
514
-----
615

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
$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)');
28+
}
29+
}
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
-----
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php echo $view['form']->start($form) ?>
2+
<?php echo $view['form']->widget($form) ?>
3+
<?php echo $view['form']->end($form) ?>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php if (!isset($render_rest) || $render_rest): ?>
2+
<?php echo $view['form']->rest($form) ?>
3+
<?php endif ?>
4+
</form>

0 commit comments

Comments
 (0)