8000 bug #16679 [Form] Disabled view data validation if "data_class" is se… · symfony/symfony@99c05b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 99c05b3

Browse files
committed
bug #16679 [Form] Disabled view data validation if "data_class" is set to null (webmozart)
This PR was merged into the 2.7 branch. Discussion ---------- [Form] Disabled view data validation if "data_class" is set to null | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #14877 | License | MIT | Doc PR | - After this PR, Form::setData() does not validate the view data anymore when "data_class" is set to `null`. This way it is possible to create fields with dynamic view data types (see #14877). Commits ------- f495410 [Form] Disabled view data validation if "data_class" is set to null
2 parents 0a40b54 + f495410 commit 99c05b3

File tree

2 files changed

+9
-19
lines changed

2 files changed

+9
-19
lines changed

src/Symfony/Component/Form/Form.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -356,21 +356,11 @@ public function setData($modelData)
356356
if (!FormUtil::isEmpty($viewData)) {
357357
$dataClass = $this->config->getDataClass();
358358

359-
$actualType = is_object($viewData) ? 'an instance of class '.get_class($viewData) : 'a(n) '.gettype($viewData);
360-
361-
if (null === $dataClass && is_object($viewData) && !$viewData instanceof \ArrayAccess) {
362-
$expectedType = 'scalar, array or an instance of \ArrayAccess';
363-
364-
throw new LogicException(
365-
'The form\'s view data is expected to be of type '.$expectedType.', '.
366-
'but is '.$actualType.'. You '.
367-
'can avoid this error by setting the "data_class" option to '.
368-
'"'.get_class($viewData).'" or by adding a view transformer '.
369-
'that transforms '.$actualType.' to '.$expectedType.'.'
370-
);
371-
}
372-
373359
if (null !== $dataClass && !$viewData instanceof $dataClass) {
360+
$actualType = is_object($viewData)
361+
? 'an instance of class '.get_class($viewData)
362+
: 'a(n) '.gettype($viewData);
363+
374364
throw new LogicException(
375365
'The form\'s view data is expected to be an instance of class '.
376366
$dataClass.', but is '.$actualType.'. You can avoid this error '.

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -840,19 +840,19 @@ public function testGetPropertyPathDefaultsToIndexedNameIfDataClassOfFirstParent
840840
$this->assertEquals(new PropertyPath('[name]'), $form->getPropertyPath());
841841
}
842842

843-
/**
844-
* @expectedException \Symfony\Component\Form\Exception\LogicException
845-
*/
846-
public function testViewDataMustNotBeObjectIfDataClassIsNull()
843+
public function testViewDataMayBeObjectIfDataClassIsNull()
847844
{
845+
$object = new \stdClass();
848846
$config = new FormConfigBuilder('name', null, $this->dispatcher);
849847
$config->addViewTransformer(new FixedDataTransformer(array(
850848
'' => '',
851-
'foo' => new \stdClass(),
849+
'foo' => $object,
852850
)));
853851
$form = new Form($config);
854852

855853
$form->setData('foo');
854+
855+
$this->assertSame($object, $form->getViewData());
856856
}
857857

858858
public function testViewDataMayBeArrayAccessIfDataClassIsNull()

0 commit comments

Comments
 (0)
0