Skip to content

[Form] add 'force_submit' option to FormType #18053

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/NativeRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
24 changes: 23 additions & 1 deletion src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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())
Expand All @@ -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())
Expand Down