Skip to content

Commit bbe1581

Browse files
committed
Allow specifying a callable as the CollectionType "prototype_name" option
1 parent 9f77ba3 commit bbe1581

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/Symfony/Component/Form/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+
7.2
5+
---
6+
7+
* Allow specifying `prototype_name` option of `CollectionType` as a `callable` (resolved value stored as `resolved_prototype_name` config attribute)
8+
49
7.1
510
---
611

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
2525
{
2626
$resizePrototypeOptions = null;
2727
if ($options['allow_add'] && $options['prototype']) {
28+
$prototypeName = \is_callable($options['prototype_name']) ? $options['prototype_name']() : $options['prototype_name'];
29+
$builder->setAttribute('resolved_prototype_name', $prototypeName);
30+
2831
$resizePrototypeOptions = array_replace($options['entry_options'], $options['prototype_options']);
2932
$prototypeOptions = array_replace([
3033
'required' => $options['required'],
31-
'label' => $options['prototype_name'].'label__',
34+
'label' => $prototypeName.'label__',
3235
], $resizePrototypeOptions);
3336

3437
if (null !== $options['prototype_data']) {
3538
$prototypeOptions['data'] = $options['prototype_data'];
3639
}
3740

38-
$prototype = $builder->create($options['prototype_name'], $options['entry_type'], $prototypeOptions);
41+
$prototype = $builder->create($prototypeName, $options['entry_type'], $prototypeOptions);
3942
$builder->setAttribute('prototype', $prototype->getForm());
4043
}
4144

@@ -121,6 +124,7 @@ public function configureOptions(OptionsResolver $resolver): void
121124
$resolver->setNormalizer('entry_options', $entryOptionsNormalizer);
122125

123126
$resolver->setAllowedTypes('delete_empty', ['bool', 'callable']);
127+
$resolver->setAllowedTypes('prototype_name', ['string', 'callable']);
124128
$resolver->setAllowedTypes('prototype_options', 'array');
125129
$resolver->setAllowedTypes('keep_as_list', ['bool']);
126130
}

0 commit comments

Comments
 (0)