[Form] Disabled view data validation if "data_class" is set to null

This commit is contained in:
Bernhard Schussek 2015-11-26 10:11:54 +01:00
parent d1a50a2b61
commit f495410d25
2 changed files with 9 additions and 19 deletions

View File

@ -356,21 +356,11 @@ class Form implements \IteratorAggregate, FormInterface
if (!FormUtil::isEmpty($viewData)) { if (!FormUtil::isEmpty($viewData)) {
$dataClass = $this->config->getDataClass(); $dataClass = $this->config->getDataClass();
$actualType = is_object($viewData) ? 'an instance of class '.get_class($viewData) : 'a(n) '.gettype($viewData);
if (null === $dataClass && is_object($viewData) && !$viewData instanceof \ArrayAccess) {
$expectedType = 'scalar, array or an instance of \ArrayAccess';
throw new LogicException(
'The form\'s view data is expected to be of type '.$expectedType.', '.
'but is '.$actualType.'. You '.
'can avoid this error by setting the "data_class" option to '.
'"'.get_class($viewData).'" or by adding a view transformer '.
'that transforms '.$actualType.' to '.$expectedType.'.'
);
}
if (null !== $dataClass && !$viewData instanceof $dataClass) { if (null !== $dataClass && !$viewData instanceof $dataClass) {
$actualType = is_object($viewData)
? 'an instance of class '.get_class($viewData)
: 'a(n) '.gettype($viewData);
throw new LogicException( throw new LogicException(
'The form\'s view data is expected to be an instance of class '. 'The form\'s view data is expected to be an instance of class '.
$dataClass.', but is '.$actualType.'. You can avoid this error '. $dataClass.', but is '.$actualType.'. You can avoid this error '.

View File

@ -840,19 +840,19 @@ class SimpleFormTest extends AbstractFormTest
$this->assertEquals(new PropertyPath('[name]'), $form->getPropertyPath()); $this->assertEquals(new PropertyPath('[name]'), $form->getPropertyPath());
} }
/** public function testViewDataMayBeObjectIfDataClassIsNull()
* @expectedException \Symfony\Component\Form\Exception\LogicException
*/
public function testViewDataMustNotBeObjectIfDataClassIsNull()
{ {
$object = new \stdClass();
$config = new FormConfigBuilder('name', null, $this->dispatcher); $config = new FormConfigBuilder('name', null, $this->dispatcher);
$config->addViewTransformer(new FixedDataTransformer(array( $config->addViewTransformer(new FixedDataTransformer(array(
'' => '', '' => '',
'foo' => new \stdClass(), 'foo' => $object,
))); )));
$form = new Form($config); $form = new Form($config);
$form->setData('foo'); $form->setData('foo');
$this->assertSame($object, $form->getViewData());
} }
public function testViewDataMayBeArrayAccessIfDataClassIsNull() public function testViewDataMayBeArrayAccessIfDataClassIsNull()