Skip to content

Commit

Permalink
[Form] Fixed Form::bindRequest() when used on a form without children
Browse files Browse the repository at this point in the history
  • Loading branch information
webmozart committed Feb 10, 2012
1 parent 11e3516 commit 6a45a41
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
23 changes: 15 additions & 8 deletions src/Symfony/Component/Form/Form.php
Expand Up @@ -578,15 +578,22 @@ public function bindRequest(Request $request)
case 'DELETE':
case 'PATCH':
if ('' === $this->getName()) {
$data = array_replace_recursive(
$request->request->all(),
$request->files->all()
);
// Form bound without name
$params = $request->request->all();
$files = $request->files->all();
} elseif ($this->hasChildren()) {
// Form bound with name and children
$params = $request->request->get($this->getName(), array());
$files = $request->files->get($this->getName(), array());
} else {
$data = array_replace_recursive(
$request->request->get($this->getName(), array()),
$request->files->get($this->getName(), array())
);
// Form bound with name, but without children
$params = $request->request->get($this->getName(), null);
$files = $request->files->get($this->getName(), null);
}
if (is_array($params) && is_array($files)) {
$data = array_replace_recursive($params, $files);
} else {
$data = $params ?: $files;
}
break;
case 'GET':
Expand Down
59 changes: 58 additions & 1 deletion tests/Symfony/Tests/Component/Form/FormTest.php
Expand Up @@ -936,7 +936,6 @@ public function testBindPostOrPutRequestWithEmptyRootFormName($method)

$values = array(
'name' => 'Bernhard',
'image' => array('filename' => 'foobar.png'),
'extra' => 'data',
);

Expand Down Expand Up @@ -969,6 +968,64 @@ public function testBindPostOrPutRequestWithEmptyRootFormName($method)
unlink($path);
}

/**
* @dataProvider requestMethodProvider
*/
public function testBindPostOrPutRequestWithSingleFieldForm($method)
{
$path = tempnam(sys_get_temp_dir(), 'sf2');
touch($path);

$files = array(
'image' => array(
'error' => UPLOAD_ERR_OK,
'name' => 'upload.png',
'size' => 123,
'tmp_name' => $path,
'type' => 'image/png',
),
);

$request = new Request(array(), array(), array(), array(), $files, array(
'REQUEST_METHOD' => $method,
));

$form = $this->getBuilder('image')->getForm();

$form->bindRequest($request);

$file = new UploadedFile($path, 'upload.png', 'image/png', 123, UPLOAD_ERR_OK);

$this->assertEquals($file, $form->getData());

unlink($path);
}

/**
* @dataProvider requestMethodProvider
*/
public function testBindPostOrPutRequestWithSingleFieldFormUploadedFile($method)
{
$path = tempnam(sys_get_temp_dir(), 'sf2');
touch($path);

$values = array(
'name' => 'Bernhard',
);

$request = new Request(array(), $values, array(), array(), array(), array(
'REQUEST_METHOD' => $method,
));

$form = $this->getBuilder('name')->getForm();

$form->bindRequest($request);

$this->assertEquals('Bernhard', $form->getData());

unlink($path);
}

public function testBindGetRequest()
{
$values = array(
Expand Down

0 comments on commit 6a45a41

Please sign in to comment.