From 82c221a1dd2be567cffc1b40c9bd12d0c2995c3b Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Tue, 22 May 2012 18:24:59 +0200 Subject: [PATCH] [Form] Fixed strict "data_class" check to work with instances of \ArrayAccess --- src/Symfony/Component/Form/Form.php | 8 ++------ src/Symfony/Component/Form/Tests/FormTest.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 0ad79f70c7..a159aea6cc 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -340,12 +340,8 @@ class Form implements \IteratorAggregate, FormInterface if (!empty($clientData)) { $dataClass = $this->config->getDataClass(); - if (null === $dataClass && is_object($clientData)) { - $expectedType = 'scalar'; - - if (count($this->children) > 0 && $this->config->getDataMapper()) { - $expectedType = 'array'; - } + if (null === $dataClass && is_object($clientData) && !$clientData instanceof \ArrayAccess) { + $expectedType = 'scalar, array or an instance of \ArrayAccess'; throw new FormException( 'The form\'s client data is expected to be of type ' . $expectedType . ', ' . diff --git a/src/Symfony/Component/Form/Tests/FormTest.php b/src/Symfony/Component/Form/Tests/FormTest.php index a15c9d6a40..389d5b4747 100644 --- a/src/Symfony/Component/Form/Tests/FormTest.php +++ b/src/Symfony/Component/Form/Tests/FormTest.php @@ -1249,6 +1249,21 @@ class FormTest extends \PHPUnit_Framework_TestCase $form->setData('foo'); } + public function testClientDataMayBeArrayAccessIfDataClassIsNull() + { + $arrayAccess = $this->getMock('\ArrayAccess'); + $config = new FormConfig('name', null, $this->dispatcher); + $config->appendClientTransformer(new FixedDataTransformer(array( + '' => '', + 'foo' => $arrayAccess, + ))); + $form = new Form($config); + + $form->setData('foo'); + + $this->assertSame($arrayAccess, $form->getClientData()); + } + /** * @expectedException Symfony\Component\Form\Exception\FormException */