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

[Form] Added "collection_entry" block prefix to CollectionType entries #36088

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

Merged
merged 1 commit into from
Mar 17, 2020
Merged
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
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
5.1.0
-----

* Added `collection_entry` block prefix to `CollectionType` entries
* Added a `choice_filter` option to `ChoiceType`
* Added argument `callable|null $filter` to `ChoiceListFactoryInterface::createListFromChoices()` and `createListFromLoader()` - not defining them is deprecated.
* Added a `ChoiceList` facade to leverage explicit choice list caching based on options
Expand Down
28 changes: 26 additions & 2 deletions 28 src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,32 @@ public function buildView(FormView $view, FormInterface $form, array $options)
*/
public function finishView(FormView $view, FormInterface $form, array $options)
{
if ($form->getConfig()->hasAttribute('prototype') && $view->vars['prototype']->vars['multipart']) {
$view->vars['multipart'] = true;
$prefixOffset = -1;
// check if the entry type also defines a block prefix
/** @var FormInterface $entry */
foreach ($form as $entry) {
if ($entry->getConfig()->getOption('block_prefix')) {
--$prefixOffset;
}

break;
}

foreach ($view as $entryView) {
array_splice($entryView->vars['block_prefixes'], $prefixOffset, 0, 'collection_entry');
}

/** @var FormInterface $prototype */
if ($prototype = $form->getConfig()->getAttribute('prototype')) {
if ($view->vars['prototype']->vars['multipart']) {
$view->vars['multipart'] = true;
}

if ($prefixOffset > -2 && $prototype->getConfig()->getOption('block_prefix')) {
--$prefixOffset;
}

array_splice($view->vars['prototype']->vars['block_prefixes'], $prefixOffset, 0, 'collection_entry');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Form\Tests\Fixtures\Author;
use Symfony\Component\Form\Tests\Fixtures\AuthorType;
use Symfony\Component\Form\Tests\Fixtures\BlockPrefixedFooTextType;

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

public function testEntriesBlockPrefixes()
{
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
'allow_add' => true,
])
->createView()
;

$expectedBlockPrefixes = [
'form',
'text',
'collection_entry',
'_fields_entry',
];

$this->assertCount(1, $collectionView);
$this->assertSame($expectedBlockPrefixes, $collectionView[0]->vars['block_prefixes']);
$this->assertSame($expectedBlockPrefixes, $collectionView->vars['prototype']->vars['block_prefixes']);
}

public function testEntriesBlockPrefixesWithCustomBlockPrefix()
{
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
'entry_options' => ['block_prefix' => 'field'],
])
->createView()
;

$this->assertCount(1, $collectionView);
$this->assertSame([
'form',
'text',
'collection_entry',
'field',
'_fields_entry',
], $collectionView[0]->vars['block_prefixes']);
}

public function testEntriesBlockPrefixesWithCustomBlockPrefixedType()
{
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
'entry_type' => BlockPrefixedFooTextType::class,
])
->createView()
;

$this->assertCount(1, $collectionView);
$this->assertSame([
'form',
'block_prefixed_foo_text',
'collection_entry',
'foo',
'_fields_entry',
], $collectionView[0]->vars['block_prefixes']);
}

public function testSubmitNull($expected = null, $norm = null, $view = null)
{
parent::testSubmitNull([], [], []);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class BlockPrefixedFooTextType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('block_prefix', 'foo');
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.