[Form] Fixed strict "data_class" check to work with instances of \ArrayAccess

This commit is contained in:
Bernhard Schussek 2012-05-22 18:24:59 +02:00
parent 517ae43fe9
commit 82c221a1dd
2 changed files with 17 additions and 6 deletions

View File

@ -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 . ', ' .

View File

@ -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
*/