merged branch bschussek/issue2553 (PR #3319)

Commits
-------

6a45a41 [Form] Fixed Form::bindRequest() when used on a form without children

Discussion
----------

[Form] Fixed Form::bindRequest() when used on a form without children

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #2553
Todo: -

![Travis Build Status](https://secure.travis-ci.org/bschussek/symfony.png?branch=issue2553)
This commit is contained in:
Fabien Potencier 2012-02-10 12:55:15 +01:00
commit feb0dfd55e
2 changed files with 73 additions and 9 deletions

View File

@ -578,15 +578,22 @@ class Form implements \IteratorAggregate, FormInterface
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':

View File

@ -936,7 +936,6 @@ class FormTest extends \PHPUnit_Framework_TestCase
$values = array(
'name' => 'Bernhard',
'image' => array('filename' => 'foobar.png'),
'extra' => 'data',
);
@ -969,6 +968,64 @@ class FormTest extends \PHPUnit_Framework_TestCase
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(