Skip to content

Allow for the theming of specific fields within a collection type #2806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions UPGRADE-2.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@

### Form

* You can now theme all form fields of a collection type by referencing it as
`_collectionChildName_fieldName`, e.g.: `_author_firstName`

#### BC Breaks in Form Types and Options

* A third argument `$options` was added to the methods `buildView()` and
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ public function finishView(FormViewInterface $view, FormInterface $form, array $
if ($form->getConfig()->hasAttribute('prototype') && $view->getVar('prototype')->getVar('multipart')) {
$view->setVar('multipart', true);
}

foreach($view as $rowName => $rowView) {
$rowTypes = $form->get($rowName)->getTypes();
$rowTypeName = end($rowTypes)->getName();

if ($rowView->getVar('compound')) {
foreach($rowView as $childName => $childView) {
$types = $childView->getVar('types');
$types[] = sprintf("_%s_%s", $rowTypeName, $childName);
$childView->setVar('types', $types);
}
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,15 @@ public function testPrototypeDefaultLabel()

$this->assertSame('__test__label__', $form->createView()->getVar('prototype')->getVar('label'));
}

public function testViewTypesAreCorrectlySet()
{
$form = $this->factory->create('collection', array(new \Symfony\Component\Form\Tests\Fixtures\Author()), array(
'type' => new \Symfony\Component\Form\Tests\Fixtures\AuthorType()
));

$view = $form->createView();
var_dump( $view->get(0)->get('firstName')->getVar('types'));
$this->assertContains('_author_firstName', $view->get(0)->get('firstName')->getVar('types'));
}
}