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 5fbc7ca

Browse filesBrowse files
committed
[Form] Added "collection_entry" block prefix to CollectionType entries
1 parent df1caae commit 5fbc7ca
Copy full SHA for 5fbc7ca

File tree

3 files changed

+106
-2
lines changed
Filter options

3 files changed

+106
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php
+26-2Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,32 @@ public function buildView(FormView $view, FormInterface $form, array $options)
7272
*/
7373
public function finishView(FormView $view, FormInterface $form, array $options)
7474
{
75-
if ($form->getConfig()->hasAttribute('prototype') && $view->vars['prototype']->vars['multipart']) {
76-
$view->vars['multipart'] = true;
75+
$prefixOffset = -1;
76+
// check if the entry type also defines a block prefix
77+
/** @var FormInterface $entry */
78+
foreach ($form as $entry) {
79+
if ($entry->getConfig()->getOption('block_prefix')) {
80+
--$prefixOffset;
81+
}
82+
83+
break;
84+
}
85+
86+
foreach ($view as $entryView) {
87+
array_splice($entryView->vars['block_prefixes'], $prefixOffset, 0, 'collection_entry');
88+
}
89+
90+
/** @var FormInterface $prototype */
91+
if ($prototype = $form->getConfig()->getAttribute('prototype')) {
92+
if ($view->vars['prototype']->vars['multipart']) {
93+
$view->vars['multipart'] = true;
94+
}
95+
96+
if ($prefixOffset > -2 && $prototype->getConfig()->getOption('block_prefix')) {
97+
--$prefixOffset;
98+
}
99+
100+
array_splice($view->vars['prototype']->vars['block_prefixes'], $prefixOffset, 0, 'collection_entry');
77101
}
78102
}
79103

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Form\Tests\Fixtures\Author;
1515
use Symfony\Component\Form\Tests\Fixtures\AuthorType;
16+
use Symfony\Component\Form\Tests\Fixtures\BlockPrefixedFooTextType;
1617

1718
class CollectionTypeTest extends BaseTypeTest
1819
{
@@ -404,6 +405,62 @@ public function testPrototypeNotOverrideRequiredByEntryOptionsInFavorOfParent()
404405
$this->assertFalse($child->createView()->vars['prototype']->vars['required'], '"Prototype" should not be required');
405406
}
406407

408+
public function testEntriesBlockPrefixes()
409+
{
410+
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
411+
'allow_add' => true,
412+
])
413+
->createView()
414+
;
415+
416+
$expectedBlockPrefixes = [
417+
'form',
418+
'text',
419+
'collection_entry',
420+
'_fields_entry',
421+
];
422+
423+
$this->assertCount(1, $collectionView);
424+
$this->assertSame($expectedBlockPrefixes, $collectionView[0]->vars['block_prefixes']);
425+
$this->assertSame($expectedBlockPrefixes, $collectionView->vars['prototype']->vars['block_prefixes']);
426+
}
427+
428+
public function testEntriesBlockPrefixesWithCustomBlockPrefix()
429+
{
430+
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
431+
'entry_options' => ['block_prefix' => 'field'],
432+
])
433+
->createView()
434+
;
435+
436+
$this->assertCount(1, $collectionView);
437+
$this->assertSame([
438+
'form',
439+
'text',
440+
'collection_entry',
441+
'field',
442+
'_fields_entry',
443+
], $collectionView[0]->vars['block_prefixes']);
444+
}
445+
446+
public function testEntriesBlockPrefixesWithCustomBlockPrefixedType()
447+
{
448+
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
449+
'entry_type' => BlockPrefixedFooTextType::class,
450+
])
451+
->createView()
452+
;
453+
454+
$this->assertCount(1, $collectionView);
455+
$this->assertSame([
456+
'form',
457+
'block_prefixed_foo_text',
458+
'collection_entry',
459+
'foo',
460+
'_fields_entry',
461+
], $collectionView[0]->vars['block_prefixes']);
462+
}
463+
407464
public function testSubmitNull($expected = null, $norm = null, $view = null)
408465
{
409466
parent::testSubmitNull([], [], []);
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\Tests\Fixtures;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
class BlockPrefixedFooTextType extends AbstractType
18+
{
19+
public function configureOptions(OptionsResolver $resolver)
20+
{
21+
$resolver->setDefault('block_prefix', 'foo');
22+
}
23+
}

0 commit comments

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