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:
Fabien Potencier 2015-11-26 18:18:04 +01:00
commit 99c05b3fc4
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()