Skip to content

Commit 82c221a

Browse files
committed
[Form] Fixed strict "data_class" check to work with instances of \ArrayAccess
1 parent 517ae43 commit 82c221a

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/Symfony/Component/Form/Form.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,8 @@ public function setData($appData)
340340
if (!empty($clientData)) {
341341
$dataClass = $this->config->getDataClass();
342342

343-
if (null === $dataClass && is_object($clientData)) {
344-
$expectedType = 'scalar';
345-
346-
if (count($this->children) > 0 && $this->config->getDataMapper()) {
347-
$expectedType = 'array';
348-
}
343+
if (null === $dataClass && is_object($clientData) && !$clientData instanceof \ArrayAccess) {
344+
$expectedType = 'scalar, array or an instance of \ArrayAccess';
349345

350346
throw new FormException(
351347
'The form\'s client data is expected to be of type ' . $expectedType . ', ' .

src/Symfony/Component/Form/Tests/FormTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,21 @@ public function testClientDataMustNotBeObjectIfDataClassIsNull()
12491249
$form->setData('foo');
12501250
}
12511251

1252+
public function testClientDataMayBeArrayAccessIfDataClassIsNull()
1253+
{
1254+
$arrayAccess = $this->getMock('\ArrayAccess');
1255+
$config = new FormConfig('name', null, $this->dispatcher);
1256+
$config->appendClientTransformer(new FixedDataTransformer(array(
1257+
'' => '',
1258+
'foo' => $arrayAccess,
1259+
)));
1260+
$form = new Form($config);
1261+
1262+
$form->setData('foo');
1263+
1264+
$this->assertSame($arrayAccess, $form->getClientData());
1265+
}
1266+
12521267
/**
12531268
* @expectedException Symfony\Component\Form\Exception\FormException
12541269
*/

0 commit comments

Comments
 (0)