diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index db7cf28fdbaaf..d45b9b78a1e27 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * deprecated the "choices_as_values" option of ChoiceType * deprecated support for data objects that implements both `Traversable` and `ArrayAccess` in `ResizeFormListener::preSubmit` method + * added "force_submit" option to FormType 3.0.0 ----- diff --git a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php index ff8d0b4fdecf7..ebfbe6aa344b7 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/FormType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/FormType.php @@ -172,6 +172,9 @@ public function configureOptions(OptionsResolver $resolver) 'action' => '', 'attr' => array(), 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', + // Allow submission in request handlers if the form name does not belong + // to submitted data keys, see https://github.com/symfony/symfony/issues/16491 + 'force_submit' => false, )); $resolver->setAllowedTypes('label_attr', 'array'); diff --git a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php index 98bbd4b9ce508..8f3db0828d216 100644 --- a/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php +++ b/src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php @@ -58,7 +58,7 @@ public function handleRequest(FormInterface $form, $request = null) // 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 || $form->getConfig()->getOption('force_submit')) { $data = $request->query->all(); } else { // Don't submit GET requests if the form's name does not exist @@ -89,7 +89,7 @@ public function handleRequest(FormInterface $form, $request = null) return; } - if ('' === $name) { + if ('' === $name || $form->getConfig()->getOption('force_submit')) { $params = $request->request->all(); $files = $request->files->all(); } elseif ($request->request->has($name) || $request->files->has($name)) { diff --git a/src/Symfony/Component/Form/NativeRequestHandler.php b/src/Symfony/Component/Form/NativeRequestHandler.php index c9a76858ddf23..ddea6a95d2ecf 100644 --- a/src/Symfony/Component/Form/NativeRequestHandler.php +++ b/src/Symfony/Component/Form/NativeRequestHandler.php @@ -66,7 +66,7 @@ public function handleRequest(FormInterface $form, $request = null) // 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 || $form->getConfig()->getOption('force_submit')) { $data = $_GET; } else { // Don't submit GET requests if the form's name does not exist @@ -102,7 +102,7 @@ public function handleRequest(FormInterface $form, $request = null) $fixedFiles[$fileKey] = self::stripEmptyFiles(self::fixPhpFilesArray($file)); } - if ('' === $name) { + if ('' === $name || $form->getConfig()->getOption('force_submit')) { $params = $_POST; $files = $fixedFiles; } elseif (array_key_exists($name, $_POST) || array_key_exists($name, $fixedFiles)) { diff --git a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php index 022b5148e9e51..d108a8b1d03a2 100644 --- a/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php +++ b/src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php @@ -117,6 +117,24 @@ public function testDoNoSubmitSimpleFormIfNameNotInRequestAndNotGetRequest($meth $this->requestHandler->handleRequest($form, $this->request); } + /** + * @dataProvider methodProvider + */ + public function testForceSubmitSimpleFormIfNameNotInRequest($method) + { + $form = $this->getMockForm('param1', $method, false, true); + + $submittedData = array('test'); + + $this->setRequestData($method, $submittedData); + + $form->expects($this->once()) + ->method('submit') + ->with($submittedData); + + $this->requestHandler->handleRequest($form, $this->request); + } + /** * @dataProvider methodExceptGetProvider */ @@ -361,7 +379,7 @@ abstract protected function getRequestHandler(); abstract protected function getMockFile($suffix = ''); - protected function getMockForm($name, $method = null, $compound = true) + protected function getMockForm($name, $method = null, $compound = true, $forceSubmit = false) { $config = $this->getMock('Symfony\Component\Form\FormConfigInterface'); $config->expects($this->any()) @@ -370,6 +388,10 @@ protected function getMockForm($name, $method = null, $compound = true) $config->expects($this->any()) ->method('getCompound') ->will($this->returnValue($compound)); + $config->expects($this->any()) + ->method('getOption') + ->with('force_submit') + ->will($this->returnValue($forceSubmit)); $form = $this->getMock('Symfony\Component\Form\Test\FormInterface'); $form->expects($this->any())