Skip to content

[2.3] [Form] Implemented form processors #6522

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 5 commits into from
Apr 19, 2013
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
2 changes: 1 addition & 1 deletion UPGRADE-2.3.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
UPGRADE FROM 2.2 to 2.3
UPGRADE FROM 2.2 to 2.3
=======================

### Form
Expand Down
141 changes: 141 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,97 @@ UPGRADE FROM 2.x to 3.0
`DebugClassLoader`. The difference is that the constructor now takes a
loader to wrap.

### Form

* Passing a `Symfony\Component\HttpFoundation\Request` instance to
`FormInterface::bind()` was disabled. You should use
`FormInterface::process()` instead.

Before:

```
if ('POST' === $request->getMethod()) {
$form->bind($request);

if ($form->isValid()) {
// ...
}
}
```

After:

```
if ($form->process($request)->isValid()) {
// ...
}
```

If you want to test whether the form was submitted separately, you can use
the method `isBound()`:

```
if ($form->process($request)->isBound()) {
// ...

if ($form->isValid()) {
// ...
}
}
```

### FrameworkBundle

* The `enctype` method of the `form` helper was removed. You should use the
new method `start` instead.

Before:

```
<form method="post" action="http://example.com" <?php echo $view['form']->enctype($form) ?>>
...
</form>
```

After:

```
<?php echo $view['form']->start($form) ?>
...
<?php echo $view['form']->end($form) ?>
```

The method and action of the form default to "POST" and the current
document. If you want to change these values, you can set them explicitly in
the controller.

Alternative 1:

```
$form = $this->createForm('my_form', $formData, array(
'method' => 'PUT',
'action' => $this->generateUrl('target_route'),
));
```

Alternative 2:

```
$form = $this->createFormBuilder($formData)
// ...
->setMethod('PUT')
->setAction($this->generateUrl('target_route'))
->getForm();
```

It is also possible to override the method and the action in the template:

```
<?php echo $view['form']->start($form, array('method' => 'GET', 'action' => 'http://example.com')) ?>
...
<?php echo $view['form']->end($form) ?>
```

### HttpKernel

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

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

* The `form_enctype` helper was removed. You should use the new `form_start`
function instead.

Before:

```
<form method="post" action="http://example.com" {{ form_enctype(form) }}>
...
</form>
```

After:

```
{{ form_start(form) }}
...
{{ form_end(form) }}
```

The method and action of the form default to "POST" and the current
document. If you want to change these values, you can set them explicitly in
the controller.

Alternative 1:

```
$form = $this->createForm('my_form', $formData, array(
'method' => 'PUT',
'action' => $this->generateUrl('target_route'),
));
```

Alternative 2:

```
$form = $this->createFormBuilder($formData)
// ...
->setMethod('PUT')
->setAction($this->generateUrl('target_route'))
->getForm();
```

It is also possible to override the method and the action in the template:

```
{{ form_start(form, {'method': 'GET', 'action': 'http://example.com'}) }}
...
{{ form_end(form) }}
```

### Yaml

* The ability to pass file names to `Yaml::parse()` has been removed.
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Bridge/Twig/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

2.3.0
-----

* added helpers form(), form_start() and form_end()
* deprecated form_enctype() in favor of form_start()

2.2.0
-----

Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Bridge/Twig/Extension/FormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ public function getTokenParsers()
public function getFunctions()
{
return array(
'form_enctype' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
'form_enctype' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\FormEnctypeNode', array('is_safe' => array('html'))),
'form_widget' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
'form_errors' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
'form_label' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
'form_row' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
'form_rest' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\SearchAndRenderBlockNode', array('is_safe' => array('html'))),
'form_start' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\RenderBlockNode', array('is_safe' => array('html'))),
'form_end' => new \Twig_Function_Node('Symfony\Bridge\Twig\Node\RenderBlockNode', array('is_safe' => array('html'))),
'csrf_token' => new \Twig_Function_Method($this, 'renderer->renderCsrfToken'),
);
}
Expand Down
31 changes: 31 additions & 0 deletions src/Symfony/Bridge/Twig/Node/FormEnctypeNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\Node;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @deprecated Deprecated since version 2.3, to be removed in 3.0. Use
* the helper "form_start()" instead.
*/
class FormEnctypeNode extends SearchAndRenderBlockNode
{
public function compile(\Twig_Compiler $compiler)
{
parent::compile($compiler);

$compiler->raw(";\n");

// Uncomment this as soon as the deprecation note should be shown
// $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)');
}
}
42 changes: 42 additions & 0 deletions src/Symfony/Bridge/Twig/Node/RenderBlockNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\Node;

/**
* Compiles a call to {@link FormRendererInterface::renderBlock()}.
*
* The function name is used as block name. For example, if the function name
* is "foo", the block "foo" will be rendered.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class RenderBlockNode extends \Twig_Node_Expression_Function
{
public function compile(\Twig_Compiler $compiler)
{
$compiler->addDebugInfo($this);
$arguments = iterator_to_array($this->getNode('arguments'));
$compiler->write('$this->env->getExtension(\'form\')->renderer->renderBlock(');

if (isset($arguments[0])) {
$compiler->subcompile($arguments[0]);
$compiler->raw(', \'' . $this->getAttribute('name') . '\'');

if (isset($arguments[1])) {
$compiler->raw(', ');
$compiler->subcompile($arguments[1]);
}
}

$compiler->raw(')');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,38 @@

{# Misc #}

{% block form %}
{% spaceless %}
{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}
{% endspaceless %}
{% endblock form %}

{% block form_start %}
{% spaceless %}
{% set method = method|upper %}
{% if method in ["GET", "POST"] %}
{% set form_method = method %}
{% else %}
{% set form_method = "POST" %}
{% endif %}
<form method="{{ form_method|lower }}" action="{{ action }}"{% for attrname, attrvalue in attr %} {{ attrname }}="{{ attrvalue }}"{% endfor %}{% if multipart %} enctype="multipart/form-data"{% endif %}>
{% if form_method != method %}
<input type="hidden" name="_method" value="{{ method }}" />
{% endif %}
{% endspaceless %}
{% endblock form_start %}

{% block form_end %}
{% spaceless %}
{% if not render_rest is defined or render_rest %}
{{ form_rest(form) }}
{% endif %}
</form>
{% endspaceless %}
{% endblock form_end %}

{% block form_enctype %}
{% spaceless %}
{% if multipart %}enctype="multipart/form-data"{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public function testIsChoiceSelected($expected, $choice, $value)
$this->assertSame($expected, $this->extension->isSelectedChoice($choice, $value));
}

protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
}

protected function renderEnctype(FormView $view)
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'enctype');
Expand Down Expand Up @@ -173,6 +178,16 @@ protected function renderRest(FormView $view, array $vars = array())
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars);
}

protected function renderStart(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars);
}

protected function renderEnd(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars);
}

protected function setTheme(FormView $view, array $themes)
{
$this->extension->renderer->setTheme($view, $themes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ protected function tearDown()
$this->extension = null;
}

protected function renderForm(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form', $vars);
}

protected function renderEnctype(FormView $view)
{
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'enctype');
Expand Down Expand Up @@ -109,6 +114,16 @@ protected function renderRest(FormView $view, array $vars = array())
return (string) $this->extension->renderer->searchAndRenderBlock($view, 'rest', $vars);
}

protected function renderStart(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form_start', $vars);
}

protected function renderEnd(FormView $view, array $vars = array())
{
return (string) $this->extension->renderer->renderBlock($view, 'form_end', $vars);
}

protected function setTheme(FormView $view, array $themes)
{
$this->extension->renderer->setTheme($view, $themes);
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ CHANGELOG
* added `TimedPhpEngine`
* added `--clean` option the the `translation:update` command
* added `http_method_override` option
* added support for default templates per render tag
* added FormHelper::form(), FormHelper::start() and FormHelper::end()
* deprecated FormHelper::enctype() in favor of FormHelper::start()

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

2.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php echo $view['form']->start($form) ?>
<?php echo $view['form']->widget($form) ?>
<?php echo $view['form']->end($form) ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php if (!isset($render_rest) || $render_rest): ?>
<?php echo $view['form']->rest($form) ?>
<?php endif ?>
</form>
Loading