From 19b483f2eacb118592c3f7cc7e654148b341124e Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 22 Aug 2013 13:38:57 +0200 Subject: [PATCH 1/2] [Form] Removed superfluous reset() call --- src/Symfony/Component/Form/Form.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 2cf0c9dff8..c4f744f1de 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -552,8 +552,6 @@ class Form implements \IteratorAggregate, FormInterface $submittedData = array(); } - reset($this->children); - for (reset($this->children); false !== current($this->children); next($this->children)) { $child = current($this->children); $name = key($this->children); From ccaaedfcbb274960a5e847dd0cdd5e81e41aeeb1 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Thu, 22 Aug 2013 14:10:15 +0200 Subject: [PATCH 2/2] [Form] PropertyPathMapper::mapDataToForms() *always* calls setData() on every child to ensure that all *_DATA events were fired when the initialization phase is over (except for virtual forms) --- .../Core/DataMapper/PropertyPathMapper.php | 14 +++---- .../DataMapper/PropertyPathMapperTest.php | 39 +++++++++++++++++-- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php b/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php index f691ecca21..3280e008f5 100644 --- a/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php +++ b/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php @@ -44,11 +44,9 @@ class PropertyPathMapper implements DataMapperInterface */ public function mapDataToForms($data, array $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'); } @@ -56,12 +54,14 @@ class PropertyPathMapper implements DataMapperInterface $iterator = new \RecursiveIteratorIterator($iterator); foreach ($iterator as $form) { - /* @var FormInterface $form */ + /* @var \Symfony\Component\Form\FormInterface $form */ $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()); } } } @@ -83,7 +83,7 @@ class PropertyPathMapper implements DataMapperInterface $iterator = new \RecursiveIteratorIterator($iterator); foreach ($iterator as $form) { - /* @var FormInterface $form */ + /* @var \Symfony\Component\Form\FormInterface $form */ $propertyPath = $form->getPropertyPath(); $config = $form->getConfig(); 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 8af2fd5f07..0a76ec9576 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php @@ -165,8 +165,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()) @@ -175,11 +176,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 testMapDataToFormsSkipsVirtualForms()