diff --git a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php b/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php index ae4796e5e7..ff571d6392 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php +++ b/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php @@ -43,11 +43,9 @@ class PropertyPathMapper implements DataMapperInterface */ public function mapDataToForms($data, $forms) { - if (null === $data || array() === $data) { - return; - } + $empty = null === $data || array() === $data; - if (!is_array($data) && !is_object($data)) { + if (!$empty && !is_array($data) && !is_object($data)) { throw new UnexpectedTypeException($data, 'object, array or empty'); } @@ -55,8 +53,10 @@ class PropertyPathMapper implements DataMapperInterface $propertyPath = $form->getPropertyPath(); $config = $form->getConfig(); - if (null !== $propertyPath && $config->getMapped()) { + if (!$empty && null !== $propertyPath && $config->getMapped()) { $form->setData($this->propertyAccessor->getValue($data, $propertyPath)); + } else { + $form->setData($form->getConfig()->getData()); } } } diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php index ed6d8cb01e..faa10e2a7a 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php @@ -169,8 +169,9 @@ class PropertyPathMapperTest extends \PHPUnit_Framework_TestCase $this->assertNull($form->getData()); } - public function testMapDataToFormsIgnoresEmptyData() + public function testMapDataToFormsSetsDefaultDataIfPassedDataIsNull() { + $default = new \stdClass(); $propertyPath = $this->getPropertyPath('engine'); $this->propertyAccessor->expects($this->never()) @@ -179,11 +180,43 @@ class PropertyPathMapperTest extends \PHPUnit_Framework_TestCase $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); $config->setByReference(true); $config->setPropertyPath($propertyPath); - $form = $this->getForm($config); + $config->setData($default); + + $form = $this->getMockBuilder('Symfony\Component\Form\Form') + ->setConstructorArgs(array($config)) + ->setMethods(array('setData')) + ->getMock(); + + $form->expects($this->once()) + ->method('setData') + ->with($default); $this->mapper->mapDataToForms(null, array($form)); + } - $this->assertNull($form->getData()); + public function testMapDataToFormsSetsDefaultDataIfPassedDataIsEmptyArray() + { + $default = new \stdClass(); + $propertyPath = $this->getPropertyPath('engine'); + + $this->propertyAccessor->expects($this->never()) + ->method('getValue'); + + $config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher); + $config->setByReference(true); + $config->setPropertyPath($propertyPath); + $config->setData($default); + + $form = $this->getMockBuilder('Symfony\Component\Form\Form') + ->setConstructorArgs(array($config)) + ->setMethods(array('setData')) + ->getMock(); + + $form->expects($this->once()) + ->method('setData') + ->with($default); + + $this->mapper->mapDataToForms(array(), array($form)); } public function testMapFormsToDataWritesBackIfNotByReference()