From 28eabd84f833937f5e334d0cb4bf71dc71be085f Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Tue, 7 Oct 2014 21:02:39 +0200 Subject: [PATCH] [Form] fix form handling with unconventional request methods like OPTIONS --- .../EventListener/BindRequestListener.php | 31 +++++++------------ .../HttpFoundationRequestHandler.php | 4 ++- .../Component/Form/NativeRequestHandler.php | 4 ++- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php b/src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php index 2930c5b36a..6ba976a823 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/EventListener/BindRequestListener.php @@ -13,7 +13,6 @@ namespace Symfony\Component\Form\Extension\HttpFoundation\EventListener; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormEvent; -use Symfony\Component\Form\Exception\LogicException; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Request; @@ -49,12 +48,19 @@ class BindRequestListener implements EventSubscriberInterface $name = $form->getConfig()->getName(); $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()) { - case 'POST': - case 'PUT': - case 'DELETE': - case 'PATCH': + case 'GET': + case 'HEAD': + case 'TRACE': + $data = '' === $name + ? $request->query->all() + : $request->query->get($name, $default); + + break; + + default: if ('' === $name) { // Form bound without name $params = $request->request->all(); @@ -71,19 +77,6 @@ class BindRequestListener implements EventSubscriberInterface } 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); diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index 03805fef52..98bbd4b9ce 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -55,7 +55,9 @@ class HttpFoundationRequestHandler implements RequestHandlerInterface 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) { $data = $request->query->all(); } else { diff --git a/src/Symfony/Component/Form/NativeRequestHandler.php b/src/Symfony/Component/Form/NativeRequestHandler.php index 9df9066886..c4096ee362 100644 --- a/src/Symfony/Component/Form/NativeRequestHandler.php +++ b/src/Symfony/Component/Form/NativeRequestHandler.php @@ -63,7 +63,9 @@ class NativeRequestHandler implements RequestHandlerInterface 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) { $data = $_GET; } else {