Skip to content

Commit 7748f5e

Browse files
committed
[Form] add 'force_submit' option
closes #16491. Add a `force_submit` option to BaseType with default false, to submit the form even if its name is not among submitted data keys in request handlers.
1 parent 6fb9fee commit 7748f5e

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

src/Symfony/Component/Form/Extension/Core/Type/FormType.php

+3
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ public function configureOptions(OptionsResolver $resolver)
172172
'action' => '',
173173
'attr' => array(),
174174
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
175+
// Allow submission in request handlers if the form name does not belong
176+
// to submitted data keys, see https://github.com/symfony/symfony/issues/16491
177+
'force_submit' => false,
175178
));
176179

177180
$resolver->setAllowedTypes('label_attr', 'array');

src/Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function handleRequest(FormInterface $form, $request = null)
5858
// For request methods that must not have a request body we fetch data
5959
// from the query string. Otherwise we look for data in the request body.
6060
if ('GET' === $method || 'HEAD' === $method || 'TRACE' === $method) {
61-
if ('' === $name) {
61+
if ('' === $name || $form->getConfig()->getOption('force_submit')) {
6262
$data = $request->query->all();
6363
} else {
6464
// Don't submit GET requests if the form's name does not exist
@@ -89,7 +89,7 @@ public function handleRequest(FormInterface $form, $request = null)
8989
return;
9090
}
9191

92-
if ('' === $name) {
92+
if ('' === $name || $form->getConfig()->getOption('force_submit')) {
9393
$params = $request->request->all();
9494
$files = $request->files->all();
9595
} elseif ($request->request->has($name) || $request->files->has($name)) {

src/Symfony/Component/Form/NativeRequestHandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function handleRequest(FormInterface $form, $request = null)
6666
// For request methods that must not have a request body we fetch data
6767
// from the query string. Otherwise we look for data in the request body.
6868
if ('GET' === $method || 'HEAD' === $method || 'TRACE' === $method) {
69-
if ('' === $name) {
69+
if ('' === $name || $form->getConfig()->getOption('force_submit')) {
7070
$data = $_GET;
7171
} else {
7272
// Don't submit GET requests if the form's name does not exist
@@ -102,7 +102,7 @@ public function handleRequest(FormInterface $form, $request = null)
102102
$fixedFiles[$fileKey] = self::stripEmptyFiles(self::fixPhpFilesArray($file));
103103
}
104104

105-
if ('' === $name) {
105+
if ('' === $name || $form->getConfig()->getOption('force_submit')) {
106106
$params = $_POST;
107107
$files = $fixedFiles;
108108
} elseif (array_key_exists($name, $_POST) || array_key_exists($name, $fixedFiles)) {

src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ public function testDoNoSubmitSimpleFormIfNameNotInRequestAndNotGetRequest($meth
117117
$this->requestHandler->handleRequest($form, $this->request);
118118
}
119119

120+
/**
121+
* @dataProvider methodProvider
122+
*/
123+
public function testForceSubmitSimpleFormIfNameNotInGetRequest($method)
124+
{
125+
$form = $this->getMockForm('param1', $method, false, true);
126+
127+
$submittedData = array('test');
128+
129+
$this->setRequestData($method, $submittedData);
130+
131+
$form->expects($this->once())
132+
->method('submit')
133+
->with($submittedData);
134+
135+
$this->requestHandler->handleRequest($form, $this->request);
136+
}
137+
120138
/**
121139
* @dataProvider methodExceptGetProvider
122140
*/
@@ -361,7 +379,7 @@ abstract protected function getRequestHandler();
361379

362380
abstract protected function getMockFile($suffix = '');
363381

364-
protected function getMockForm($name, $method = null, $compound = true)
382+
protected function getMockForm($name, $method = null, $compound = true, $forceSubmit = false)
365383
{
366384
$config = $this->getMock('Symfony\Component\Form\FormConfigInterface');
367385
$config->expects($this->any())
@@ -370,6 +388,10 @@ protected function getMockForm($name, $method = null, $compound = true)
370388
$config->expects($this->any())
371389
->method('getCompound')
372390
->will($this->returnValue($compound));
391+
$config->expects($this->any())
392+
->method('getOption')
393+
->with('force_submit')
394+
->will($this->returnValue($forceSubmit));
373395

374396
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
375397
$form->expects($this->any())

0 commit comments

Comments
 (0)