Skip to content

Commit d0e301b

Browse files
committed
[Form] Moved parent data inheritance from data mappers to Form
1 parent 08e906b commit d0e301b

File tree

12 files changed

+420
-178
lines changed

12 files changed

+420
-178
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ CHANGELOG
1010
* deprecated passing a Request instance to FormInterface::bind()
1111
* added options "method" and "action" to FormType
1212
* deprecated option "virtual", renamed it to "inherit_data"
13+
* deprecated VirtualFormAwareIterator in favor of InheritDataAwareIterator
14+
* [BC BREAK] removed the "array" type hint from DataMapperInterface
15+
* improved forms inheriting their parent data to actually return that data from getData(), getNormData() and getViewData()
1316

1417
2.2.0
1518
-----

DataMapperInterface.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ interface DataMapperInterface
1919
/**
2020
* Maps properties of some data to a list of forms.
2121
*
22-
* @param mixed $data Structured data.
23-
* @param array $forms A list of {@link FormInterface} instances.
22+
* @param mixed $data Structured data.
23+
* @param FormInterface[] $forms A list of {@link FormInterface} instances.
2424
*
2525
* @throws Exception\UnexpectedTypeException if the type of the data parameter is not supported.
2626
*/
27-
public function mapDataToForms($data, array $forms);
27+
public function mapDataToForms($data, $forms);
2828

2929
/**
3030
* Maps the data of a list of forms into the properties of some data.
3131
*
32-
* @param array $forms A list of {@link FormInterface} instances.
33-
* @param mixed $data Structured data.
32+
* @param FormInterface[] $forms A list of {@link FormInterface} instances.
33+
* @param mixed $data Structured data.
3434
*
3535
* @throws Exception\UnexpectedTypeException if the type of the data parameter is not supported.
3636
*/
37-
public function mapFormsToData(array $forms, &$data);
37+
public function mapFormsToData($forms, &$data);
3838
}

Exception/RuntimeException.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Exception;
13+
14+
/**
15+
* Base RuntimeException for the Form component.
16+
*
17+
* @author Bernhard Schussek <[email protected]>
18+
*/
19+
class RuntimeException extends \RuntimeException implements ExceptionInterface
20+
{
21+
}

Extension/Core/DataMapper/PropertyPathMapper.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Form\Extension\Core\DataMapper;
1313

1414
use Symfony\Component\Form\DataMapperInterface;
15-
use Symfony\Component\Form\Util\VirtualFormAwareIterator;
1615
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1716
use Symfony\Component\PropertyAccess\PropertyAccess;
1817
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
@@ -42,7 +41,7 @@ public function __construct(PropertyAccessorInterface $propertyAccessor = null)
4241
/**
4342
* {@inheritdoc}
4443
*/
45-
public function mapDataToForms($data, array $forms)
44+
public function mapDataToForms($data, $forms)
4645
{
4746
if (null === $data || array() === $data) {
4847
return;
@@ -52,11 +51,7 @@ public function mapDataToForms($data, array $forms)
5251
throw new UnexpectedTypeException($data, 'object, array or empty');
5352
}
5453

55-
$iterator = new VirtualFormAwareIterator($forms);
56-
$iterator = new \RecursiveIteratorIterator($iterator);
57-
58-
foreach ($iterator as $form) {
59-
/* @var \Symfony\Component\Form\FormInterface $form */
54+
foreach ($forms as $form) {
6055
$propertyPath = $form->getPropertyPath();
6156
$config = $form->getConfig();
6257

@@ -69,7 +64,7 @@ public function mapDataToForms($data, array $forms)
6964
/**
7065
* {@inheritdoc}
7166
*/
72-
public function mapFormsToData(array $forms, &$data)
67+
public function mapFormsToData($forms, &$data)
7368
{
7469
if (null === $data) {
7570
return;
@@ -79,11 +74,7 @@ public function mapFormsToData(array $forms, &$data)
7974
throw new UnexpectedTypeException($data, 'object, array or empty');
8075
}
8176

82-
$iterator = new VirtualFormAwareIterator($forms);
83-
$iterator = new \RecursiveIteratorIterator($iterator);
84-
85-
foreach ($iterator as $form) {
86-
/* @var \Symfony\Component\Form\FormInterface $form */
77+
foreach ($forms as $form) {
8778
$propertyPath = $form->getPropertyPath();
8879
$config = $form->getConfig();
8980

Extension/Validator/ViolationMapper/ViolationMapper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Form\Extension\Validator\ViolationMapper;
1313

1414
use Symfony\Component\Form\FormInterface;
15-
use Symfony\Component\Form\Util\VirtualFormAwareIterator;
15+
use Symfony\Component\Form\Util\InheritDataAwareIterator;
1616
use Symfony\Component\PropertyAccess\PropertyPathIterator;
1717
use Symfony\Component\PropertyAccess\PropertyPathBuilder;
1818
use Symfony\Component\PropertyAccess\PropertyPathIteratorInterface;
@@ -100,6 +100,9 @@ public function mapViolation(ConstraintViolation $violation, FormInterface $form
100100
$scope = $form;
101101
$it = new ViolationPathIterator($violationPath);
102102

103+
// Note: acceptsErrors() will always return true for forms inheriting
104+
// their parent data, because these forms can never be non-synchronized
105+
// (they don't do any data transformation on their own)
103106
while ($this->acceptsErrors($scope) && $it->valid() && $it->mapsForm()) {
104107
if (!$scope->has($it->current())) {
105108
// Break if we find a reference to a non-existing child
@@ -164,7 +167,7 @@ private function matchChild(FormInterface $form, PropertyPathIteratorInterface $
164167

165168
// Skip forms inheriting their parent data when iterating the children
166169
$childIterator = new \RecursiveIteratorIterator(
167-
new VirtualFormAwareIterator($form->all())
170+
new InheritDataAwareIterator($form->all())
168171
);
169172

170173
// Make the path longer until we find a matching child

0 commit comments

Comments
 (0)