Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 4 additions & 14 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,21 +356,11 @@ public function setData($modelData)
if (!FormUtil::isEmpty($viewData)) {
$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) {
$actualType = is_object($viewData)
? 'an instance of class '.get_class($viewData)
: 'a(n) '.gettype($viewData);

throw new LogicException(
'The form\'s view data is expected to be an instance of class '.
$dataClass.', but is '.$actualType.'. You can avoid this error '.
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Form/Tests/SimpleFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,19 +840,19 @@ public function testGetPropertyPathDefaultsToIndexedNameIfDataClassOfFirstParent
$this->assertEquals(new PropertyPath('[name]'), $form->getPropertyPath());
}

/**
* @expectedException \Symfony\Component\Form\Exception\LogicException
*/
public function testViewDataMustNotBeObjectIfDataClassIsNull()
public function testViewDataMayBeObjectIfDataClassIsNull()
{
$object = new \stdClass();
$config = new FormConfigBuilder('name', null, $this->dispatcher);
$config->addViewTransformer(new FixedDataTransformer(array(
'' => '',
'foo' => new \stdClass(),
'foo' => $object,
)));
$form = new Form($config);

$form->setData('foo');

$this->assertSame($object, $form->getViewData());
}

public function testViewDataMayBeArrayAccessIfDataClassIsNull()
Expand Down