Navigation Menu

Skip to content

Commit

Permalink
[Form] Fixed: If a form is not present in a request, it is not automa…
Browse files Browse the repository at this point in the history
…tically submitted
  • Loading branch information
webmozart committed Aug 1, 2013
1 parent bd30a5c commit cb5e765
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
Expand Up @@ -56,10 +56,13 @@ public function handleRequest(FormInterface $form, $request = null)
if ('' === $name) {
$params = $request->request->all();
$files = $request->files->all();
} else {
} elseif ($request->request->has($name) || $request->files->has($name)) {
$default = $form->getConfig()->getCompound() ? array() : null;
$params = $request->request->get($name, $default);
$files = $request->files->get($name, $default);
} else {
// Don't submit the form if it is not present in the request
return;
}

if (is_array($params) && is_array($files)) {
Expand Down
9 changes: 6 additions & 3 deletions src/Symfony/Component/Form/NativeRequestHandler.php
Expand Up @@ -72,10 +72,13 @@ public function handleRequest(FormInterface $form, $request = null)
if ('' === $name) {
$params = $_POST;
$files = $fixedFiles;
} else {
} elseif (array_key_exists($name, $_POST) || array_key_exists($name, $fixedFiles)) {
$default = $form->getConfig()->getCompound() ? array() : null;
$params = isset($_POST[$name]) ? $_POST[$name] : $default;
$files = isset($fixedFiles[$name]) ? $fixedFiles[$name] : $default;
$params = array_key_exists($name, $_POST) ? $_POST[$name] : $default;
$files = array_key_exists($name, $fixedFiles) ? $fixedFiles[$name] : $default;
} else {
// Don't submit the form if it is not present in the request
return;
}

if (is_array($params) && is_array($files)) {
Expand Down
14 changes: 6 additions & 8 deletions src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php
Expand Up @@ -86,35 +86,33 @@ public function testDoNotSubmitIfWrongRequestMethod($method)
/**
* @dataProvider methodExceptGetProvider
*/
public function testSubmitSimpleFormWithNullIfNameNotInRequestAndNotGetRequest($method)
public function testDoNoSubmitSimpleFormIfNameNotInRequestAndNotGetRequest($method)
{
$form = $this->getMockForm('param1', $method, false);

$this->setRequestData($method, array(
'paramx' => array(),
));

$form->expects($this->once())
->method('submit')
->with($this->identicalTo(null), 'PATCH' !== $method);
$form->expects($this->never())
->method('submit');

$this->requestHandler->handleRequest($form, $this->request);
}

/**
* @dataProvider methodExceptGetProvider
*/
public function testSubmitCompoundFormWithArrayIfNameNotInRequestAndNotGetRequest($method)
public function testDoNotSubmitCompoundFormIfNameNotInRequestAndNotGetRequest($method)
{
$form = $this->getMockForm('param1', $method, true);

$this->setRequestData($method, array(
'paramx' => array(),
));

$form->expects($this->once())
->method('submit')
->with($this->identicalTo(array()), 'PATCH' !== $method);
$form->expects($this->never())
->method('submit');

$this->requestHandler->handleRequest($form, $this->request);
}
Expand Down

0 comments on commit cb5e765

Please sign in to comment.