Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ccaaedf

Browse filesBrowse files
committed
[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)
1 parent 19b483f commit ccaaedf
Copy full SHA for ccaaedf

File tree

Expand file treeCollapse file tree

2 files changed

+43
-10
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+43
-10
lines changed

‎src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,24 @@ public function __construct(PropertyAccessorInterface $propertyAccessor = null)
4444
*/
4545
public function mapDataToForms($data, array $forms)
4646
{
47-
if (null === $data || array() === $data) {
48-
return;
49-
}
47+
$empty = null === $data || array() === $data;
5048

51-
if (!is_array($data) && !is_object($data)) {
49+
if (!$empty && !is_array($data) && !is_object($data)) {
5250
throw new UnexpectedTypeException($data, 'object, array or empty');
5351
}
5452

5553
$iterator = new VirtualFormAwareIterator($forms);
5654
$iterator = new \RecursiveIteratorIterator($iterator);
5755

5856
foreach ($iterator as $form) {
59-
/* @var FormInterface $form */
57+
/* @var \Symfony\Component\Form\FormInterface $form */
6058
$propertyPath = $form->getPropertyPath();
6159
$config = $form->getConfig();
6260

63-
if (null !== $propertyPath && $config->getMapped()) {
61+
if (!$empty && null !== $propertyPath && $config->getMapped()) {
6462
$form->setData($this->propertyAccessor->getValue($data, $propertyPath));
63+
} else {
64+
$form->setData($form->getConfig()->getData());
6565
}
6666
}
6767
}
@@ -83,7 +83,7 @@ public function mapFormsToData(array $forms, &$data)
8383
$iterator = new \RecursiveIteratorIterator($iterator);
8484

8585
foreach ($iterator as $form) {
86-
/* @var FormInterface $form */
86+
/* @var \Symfony\Component\Form\FormInterface $form */
8787
$propertyPath = $form->getPropertyPath();
8888
$config = $form->getConfig();
8989

‎src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/DataMapper/PropertyPathMapperTest.php
+36-3Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ public function testMapDataToFormsIgnoresUnmapped()
165165
$this->assertNull($form->getData());
166166
}
167167

168-
public function testMapDataToFormsIgnoresEmptyData()
168+
public function testMapDataToFormsSetsDefaultDataIfPassedDataIsNull()
169169
{
170+
$default = new \stdClass();
170171
$propertyPath = $this->getPropertyPath('engine');
171172

172173
$this->propertyAccessor->expects($this->never())
@@ -175,11 +176,43 @@ public function testMapDataToFormsIgnoresEmptyData()
175176
$config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
176177
$config->setByReference(true);
177178
$config->setPropertyPath($propertyPath);
178-
$form = $this->getForm($config);
179+
$config->setData($default);
180+
181+
$form = $this->getMockBuilder('Symfony\Component\Form\Form')
182+
->setConstructorArgs(array($config))
183+
->setMethods(array('setData'))
184+
->getMock();
185+
186+
$form->expects($this->once())
187+
->method('setData')
188+
->with($default);
179189

180190
$this->mapper->mapDataToForms(null, array($form));
191+
}
181192

182-
$this->assertNull($form->getData());
193+
public function testMapDataToFormsSetsDefaultDataIfPassedDataIsEmptyArray()
194+
{
195+
$default = new \stdClass();
196+
$propertyPath = $this->getPropertyPath('engine');
197+
198+
$this->propertyAccessor->expects($this->never())
199+
->method('getValue');
200+
201+
$config = new FormConfigBuilder('name', '\stdClass', $this->dispatcher);
202+
$config->setByReference(true);
203+
$config->setPropertyPath($propertyPath);
204+
$config->setData($default);
205+
206+
$form = $this->getMockBuilder('Symfony\Component\Form\Form')
207+
->setConstructorArgs(array($config))
208+
->setMethods(array('setData'))
209+
->getMock();
210+
211+
$form->expects($this->once())
212+
->method('setData')
213+
->with($default);
214+
215+
$this->mapper->mapDataToForms(array(), array($form));
183216
}
184217

185218
public function testMapDataToFormsSkipsVirtualForms()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.