bug #12170 [Form] fix form handling with OPTIONS request method (Tobion)

This PR was merged into the 2.3 branch.

Discussion
----------

[Form] fix form handling with OPTIONS request method

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #8282
| License       | MIT
| Doc PR        | -

The OPTIONS request is just handled as any other request method. And accoring to the spec, an options request can also contain a request body like a POST. This only applied when using the deprecated form processing with `$form->submit($request)`. The change also makes the handling consistent with the `handleRequest` behavior via https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php

Commits
-------

28eabd8 [Form] fix form handling with unconventional request methods like OPTIONS
This commit is contained in:
Fabien Potencier 2014-11-02 02:03:36 +01:00
commit ec82e32b13
3 changed files with 18 additions and 21 deletions

View File

@ -13,7 +13,6 @@ namespace Symfony\Component\Form\Extension\HttpFoundation\EventListener;
use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@ -49,12 +48,19 @@ class BindRequestListener implements EventSubscriberInterface
$name = $form->getConfig()->getName(); $name = $form->getConfig()->getName();
$default = $form->getConfig()->getCompound() ? array() : null; $default = $form->getConfig()->getCompound() ? array() : null;
// Store the bound data in case of a post request // For request methods that must not have a request body we fetch data
// from the query string. Otherwise we look for data in the request body.
switch ($request->getMethod()) { switch ($request->getMethod()) {
case 'POST': case 'GET':
case 'PUT': case 'HEAD':
case 'DELETE': case 'TRACE':
case 'PATCH': $data = '' === $name
? $request->query->all()
: $request->query->get($name, $default);
break;
default:
if ('' === $name) { if ('' === $name) {
// Form bound without name // Form bound without name
$params = $request->request->all(); $params = $request->request->all();
@ -71,19 +77,6 @@ class BindRequestListener implements EventSubscriberInterface
} }
break; break;
case 'GET':
$data = '' === $name
? $request->query->all()
: $request->query->get($name, $default);
break;
default:
throw new LogicException(sprintf(
'The request method "%s" is not supported',
$request->getMethod()
));
} }
$event->setData($data); $event->setData($data);

View File

@ -55,7 +55,9 @@ class HttpFoundationRequestHandler implements RequestHandlerInterface
return; return;
} }
if ('GET' === $method) { // For request methods that must not have a request body we fetch data
// from the query string. Otherwise we look for data in the request body.
if ('GET' === $method || 'HEAD' === $method || 'TRACE' === $method) {
if ('' === $name) { if ('' === $name) {
$data = $request->query->all(); $data = $request->query->all();
} else { } else {

View File

@ -63,7 +63,9 @@ class NativeRequestHandler implements RequestHandlerInterface
return; return;
} }
if ('GET' === $method) { // For request methods that must not have a request body we fetch data
// from the query string. Otherwise we look for data in the request body.
if ('GET' === $method || 'HEAD' === $method || 'TRACE' === $method) {
if ('' === $name) { if ('' === $name) {
$data = $_GET; $data = $_GET;
} else { } else {