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
This commit is contained in:
commit
99c05b3fc4
|
@ -356,21 +356,11 @@ class Form implements \IteratorAggregate, FormInterface
|
|||
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 '.
|
||||
|
|
|
@ -840,19 +840,19 @@ class SimpleFormTest extends AbstractFormTest
|
|||
$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()
|
||||
|
|
Reference in New Issue