Skip to content

Commit 400020b

Browse files
committed
[Form] Added "collection_entry" block prefix to CollectionType entries
1 parent df1caae commit 400020b

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ public function finishView(FormView $view, FormInterface $form, array $options)
7575
if ($form->getConfig()->hasAttribute('prototype') && $view->vars['prototype']->vars['multipart']) {
7676
$view->vars['multipart'] = true;
7777
}
78+
79+
$prefixOffset = isset($options['entry_options']['block_prefix']) ? -2 : -1;
80+
81+
foreach ($view as $entryView) {
82+
array_splice($entryView->vars['block_prefixes'], $prefixOffset, 0, 'collection_entry');
83+
}
7884
}
7985

8086
/**

src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,39 @@ public function testPrototypeNotOverrideRequiredByEntryOptionsInFavorOfParent()
404404
$this->assertFalse($child->createView()->vars['prototype']->vars['required'], '"Prototype" should not be required');
405405
}
406406

407+
public function testEntriesBlockPrefixes()
408+
{
409+
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''])
410+
->createView()
411+
;
412+
413+
$this->assertCount(1, $collectionView);
414+
$this->assertSame([
415+
'form',
416+
'text',
417+
'collection_entry',
418+
'_fields_entry',
419+
], $collectionView[0]->vars['block_prefixes']);
420+
}
421+
422+
public function testEntriesBlockPrefixesWithCustomBlockPrefix()
423+
{
424+
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
425+
'entry_options' => ['block_prefix' => 'field'],
426+
])
427+
->createView()
428+
;
429+
430+
$this->assertCount(1, $collectionView);
431+
$this->assertSame([
432+
'form',
433+
'text',
434+
'collection_entry',
435+
'field',
436+
'_fields_entry',
437+
], $collectionView[0]->vars['block_prefixes']);
438+
}
439+
407440
public function testSubmitNull($expected = null, $norm = null, $view = null)
408441
{
409442
parent::testSubmitNull([], [], []);

0 commit comments

Comments
 (0)