Skip to content

Commit 0b9d580

Browse files
feature #45605 [Form] Add prototype_options to CollectionType (michaelKaefer)
This PR was merged into the 6.1 branch. Discussion ---------- [Form] Add prototype_options to CollectionType | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | symfony/symfony-docs#16664 Allows defining options for the form for new entries (the prototype form) which differ from the options for the form for existing entries. My use case: I need this when using EasyAdminBundle. In the form for an entity `Person` I have a `CollectionType` for a related entity `Books`. There I can edit the `Books` that a `Person` already has and add new `Books`. The change would allow me to show (or disable, etc.) a form field in the `Book` form only when an existing `Book` is edited (and not show it when a new `Book` is added). As far as I can tell it is not a BC break to add this new option. I can provide another PR for the docs if this should get merged. Commits ------- b1d40c16be [Form] Add prototype_options to CollectionType
2 parents b802731 + 1d88387 commit 0b9d580

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Add a `prototype_options` option to `CollectionType`
8+
49
6.0
510
---
611

Extension/Core/Type/CollectionType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
3030
$prototypeOptions = array_replace([
3131
'required' => $options['required'],
3232
'label' => $options['prototype_name'].'label__',
33-
], $options['entry_options']);
33+
], array_replace($options['entry_options'], $options['prototype_options']));
3434

3535
if (null !== $options['prototype_data']) {
3636
$prototypeOptions['data'] = $options['prototype_data'];
@@ -120,12 +120,15 @@ public function configureOptions(OptionsResolver $resolver)
120120
'prototype_name' => '__name__',
121121
'entry_type' => TextType::class,
122122
'entry_options' => [],
123+
'prototype_options' => [],
123124
'delete_empty' => false,
124125
'invalid_message' => 'The collection is invalid.',
125126
]);
126127

127128
$resolver->setNormalizer('entry_options', $entryOptionsNormalizer);
129+
128130
$resolver->setAllowedTypes('delete_empty', ['bool', 'callable']);
131+
$resolver->setAllowedTypes('prototype_options', 'array');
129132
}
130133

131134
/**

Tests/Extension/Core/Type/CollectionTypeTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,23 @@ public function testPrototypeNotOverrideRequiredByEntryOptionsInFavorOfParent()
430430
$this->assertFalse($child->createView()->vars['prototype']->vars['required'], '"Prototype" should not be required');
431431
}
432432

433+
public function testPrototypeOptionsOverrideEntryOptions()
434+
{
435+
$form = $this->factory->create(static::TESTED_TYPE, [], [
436+
'allow_add' => true,
437+
'prototype' => true,
438+
'entry_type' => TextTypeTest::TESTED_TYPE,
439+
'entry_options' => [
440+
'help' => null,
441+
],
442+
'prototype_options' => [
443+
'help' => 'foo',
444+
],
445+
]);
446+
447+
$this->assertSame('foo', $form->createView()->vars['prototype']->vars['help']);
448+
}
449+
433450
public function testEntriesBlockPrefixes()
434451
{
435452
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [

0 commit comments

Comments
 (0)