bug #15069 [Form] Fixed: Data mappers always receive forms indexed by their names (webmozart)

This PR was merged into the 2.3 branch.

Discussion
----------

[Form] Fixed: Data mappers always receive forms indexed by their names

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

This PR facilitates writing domain-specific data mappers, since it guarantees that you can access forms by name in the data mapper methods. Currently, `Form::add()` does not set the index of the array passed to the data mapper to the form's name.

Commits
-------

86b7fe5 [Form] Fixed: Data mappers always receive forms indexed by their names
This commit is contained in:
Fabien Potencier 2015-06-30 07:54:26 +02:00
commit 38c08d1fb3
2 changed files with 2 additions and 2 deletions

View File

@ -895,7 +895,7 @@ class Form implements \IteratorAggregate, FormInterface
$child->setParent($this);
if (!$this->lockSetData && $this->defaultDataSet && !$this->config->getInheritData()) {
$iterator = new InheritDataAwareIterator(new \ArrayIterator(array($child)));
$iterator = new InheritDataAwareIterator(new \ArrayIterator(array($child->getName() => $child)));
$iterator = new \RecursiveIteratorIterator($iterator);
$this->config->getDataMapper()->mapDataToForms($viewData, $iterator);
}

View File

@ -352,7 +352,7 @@ class CompoundFormTest extends AbstractFormTest
->with('bar', $this->isInstanceOf('\RecursiveIteratorIterator'))
->will($this->returnCallback(function ($data, \RecursiveIteratorIterator $iterator) use ($child, $test) {
$test->assertInstanceOf('Symfony\Component\Form\Util\InheritDataAwareIterator', $iterator->getInnerIterator());
$test->assertSame(array($child), iterator_to_array($iterator));
$test->assertSame(array($child->getName() => $child), iterator_to_array($iterator));
}));
$form->initialize();