From 6a45a415a1b4a296f2355e28611b26b196628aad Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Fri, 10 Feb 2012 10:36:16 +0100 Subject: [PATCH] [Form] Fixed Form::bindRequest() when used on a form without children --- src/Symfony/Component/Form/Form.php | 23 +++++--- .../Symfony/Tests/Component/Form/FormTest.php | 59 ++++++++++++++++++- 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index a4403f494e45..292be246507e 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -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': diff --git a/tests/Symfony/Tests/Component/Form/FormTest.php b/tests/Symfony/Tests/Component/Form/FormTest.php index f42351b04ab3..17cfbeaa465b 100644 --- a/tests/Symfony/Tests/Component/Form/FormTest.php +++ b/tests/Symfony/Tests/Component/Form/FormTest.php @@ -936,7 +936,6 @@ public function testBindPostOrPutRequestWithEmptyRootFormName($method) $values = array( 'name' => 'Bernhard', - 'image' => array('filename' => 'foobar.png'), 'extra' => 'data', ); @@ -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(