Skip to content

Commit

Permalink
[Form] Deprecated Form::bindRequest() and replaced it by a PRE_BIND l…
Browse files Browse the repository at this point in the history
…istener
  • Loading branch information
webmozart committed Jul 9, 2012
1 parent 5f6a17b commit 7727de7
Show file tree
Hide file tree
Showing 8 changed files with 407 additions and 38 deletions.
15 changes: 15 additions & 0 deletions UPGRADE-2.1.md
Expand Up @@ -875,6 +875,7 @@
* `getClientData`
* `getChildren`
* `hasChildren`
* `bindRequest`

Before:

Expand Down Expand Up @@ -906,6 +907,20 @@
if (count($form) > 0) {
```

Instead of `bindRequest`, you should now simply call `bind`:

Before:

```
$form->bindRequest($request);
```

After:

```
$form->bind($request);
```

* The option "validation_constraint" was deprecated and will be removed
in Symfony 2.3. You should use the option "constraints" instead,
where you can pass one or more constraints for a form.
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Expand Up @@ -145,3 +145,4 @@ CHANGELOG
* DateType, TimeType and DateTimeType now show empty values again if not required
* [BC BREAK] fixed rendering of errors for DateType, BirthdayType and similar ones
* [BC BREAK] fixed: form constraints are only validated if they belong to the validated group
* deprecated `bindRequest` in `Form` and replaced it by a listener to FormEvents::PRE_BIND
@@ -0,0 +1,85 @@
<?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\Component\Form\Extension\Core\EventListener;

use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Exception\FormException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class BindRequestListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
// High priority in order to supersede other listeners
return array(FormEvents::PRE_BIND => array('preBind', 128));
}

public function preBind(FormEvent $event)
{
$form = $event->getForm();

/* @var Request $request */
$request = $event->getData();

// Only proceed if we actually deal with a Request
if (!$request instanceof Request) {
return;
}

$name = $form->getConfig()->getName();
$default = $form->getConfig()->getCompound() ? array() : null;

// Store the bound data in case of a post request
switch ($request->getMethod()) {
case 'POST':
case 'PUT':
case 'DELETE':
case 'PATCH':
if ('' === $name) {
// Form bound without name
$params = $request->request->all();
$files = $request->files->all();
} else {
$params = $request->request->get($name, $default);
$files = $request->files->get($name, $default);
}

if (is_array($params) && is_array($files)) {
$data = array_replace_recursive($params, $files);
} else {
$data = $params ?: $files;
}

break;

case 'GET':
$data = '' === $name
? $request->query->all()
: $request->query->get($name, $default);

break;

default:
throw new FormException(sprintf(
'The request method "%s" is not supported',
$request->getMethod()
));
}

$event->setData($data);
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormViewInterface;
use Symfony\Component\Form\Extension\Core\EventListener\BindRequestListener;
use Symfony\Component\Form\Extension\Core\EventListener\TrimListener;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -44,6 +45,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->setCompound($options['compound'])
->setData($options['data'])
->setDataMapper($options['compound'] ? new PropertyPathMapper() : null)
->addEventSubscriber(new BindRequestListener())
;

if ($options['trim']) {
Expand Down
39 changes: 4 additions & 35 deletions src/Symfony/Component/Form/Form.php
Expand Up @@ -585,44 +585,13 @@ public function bind($submittedData)
* @return Form This form
*
* @throws FormException if the method of the request is not one of GET, POST or PUT
*
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Use
* {@link FormConfigInterface::bind()} instead.
*/
public function bindRequest(Request $request)
{
$name = $this->config->getName();

// Store the bound data in case of a post request
switch ($request->getMethod()) {
case 'POST':
case 'PUT':
case 'DELETE':
case 'PATCH':
if ('' === $name) {
// Form bound without name
$params = $request->request->all();
$files = $request->files->all();
} elseif ($this->config->getCompound()) {
// Form bound with name and children
$params = $request->request->get($name, array());
$files = $request->files->get($name, array());
} else {
// Form bound with name, but without children
$params = $request->request->get($name, null);
$files = $request->files->get($name, null);
}
if (is_array($params) && is_array($files)) {
$data = array_replace_recursive($params, $files);
} else {
$data = $params ?: $files;
}
break;
case 'GET':
$data = '' === $name ? $request->query->all() : $request->query->get($name, array());
break;
default:
throw new FormException(sprintf('The request method "%s" is not supported', $request->getMethod()));
}

return $this->bind($data);
return $this->bind($request);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Form/Tests/AbstractFormTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form\Tests;

use Symfony\Component\Form\FormBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

abstract class AbstractFormTest extends \PHPUnit_Framework_TestCase
Expand All @@ -37,7 +38,9 @@ protected function setUp()
$this->markTestSkipped('The "EventDispatcher" component is not available');
}

$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
// We need an actual dispatcher to bind the deprecated
// bindRequest() method
$this->dispatcher = new EventDispatcher();;
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
$this->form = $this->createForm();
}
Expand Down
13 changes: 11 additions & 2 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\Extension\Core\EventListener\BindRequestListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -360,6 +361,7 @@ public function testBindPostOrPutRequest($method)
$form = $this->getBuilder('author')
->setCompound(true)
->setDataMapper($this->getDataMapper())
->addEventSubscriber(new BindRequestListener())
->getForm();
$form->add($this->getBuilder('name')->getForm());
$form->add($this->getBuilder('image')->getForm());
Expand Down Expand Up @@ -408,6 +410,7 @@ public function testBindPostOrPutRequestWithEmptyRootFormName($method)
$form = $this->getBuilder('')
->setCompound(true)
->setDataMapper($this->getDataMapper())
->addEventSubscriber(new BindRequestListener())
->getForm();
$form->add($this->getBuilder('name')->getForm());
$form->add($this->getBuilder('image')->getForm());
Expand Down Expand Up @@ -449,7 +452,9 @@ public function testBindPostOrPutRequestWithSingleChildForm($method)
'REQUEST_METHOD' => $method,
));

$form = $this->getBuilder('image')->getForm();
$form = $this->getBuilder('image')
->addEventSubscriber(new BindRequestListener())
->getForm();

$form->bindRequest($request);

Expand Down Expand Up @@ -480,7 +485,9 @@ public function testBindPostOrPutRequestWithSingleChildFormUploadedFile($method)
'REQUEST_METHOD' => $method,
));

$form = $this->getBuilder('name')->getForm();
$form = $this->getBuilder('name')
->addEventSubscriber(new BindRequestListener())
->getForm();

$form->bindRequest($request);

Expand Down Expand Up @@ -509,6 +516,7 @@ public function testBindGetRequest()
$form = $this->getBuilder('author')
->setCompound(true)
->setDataMapper($this->getDataMapper())
->addEventSubscriber(new BindRequestListener())
->getForm();
$form->add($this->getBuilder('firstName')->getForm());
$form->add($this->getBuilder('lastName')->getForm());
Expand Down Expand Up @@ -538,6 +546,7 @@ public function testBindGetRequestWithEmptyRootFormName()
$form = $this->getBuilder('')
->setCompound(true)
->setDataMapper($this->getDataMapper())
->addEventSubscriber(new BindRequestListener())
->getForm();
$form->add($this->getBuilder('firstName')->getForm());
$form->add($this->getBuilder('lastName')->getForm());
Expand Down

0 comments on commit 7727de7

Please sign in to comment.