From b1d40c16be1464f568199f442c6cca8420b7cd11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20K=C3=A4fer?= Date: Sat, 2 Apr 2022 20:30:07 +0200 Subject: [PATCH] [Form] Add prototype_options to CollectionType --- src/Symfony/Component/Form/CHANGELOG.md | 5 +++++ .../Form/Extension/Core/Type/CollectionType.php | 5 ++++- .../Extension/Core/Type/CollectionTypeTest.php | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index 22b9c84f2a458..f2f949c897110 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.1 +--- + + * Add a `prototype_options` option to `CollectionType` + 6.0 --- diff --git a/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php b/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php index 480d31dc94afd..4ec0f1be70f55 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/CollectionType.php @@ -30,7 +30,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) $prototypeOptions = array_replace([ 'required' => $options['required'], 'label' => $options['prototype_name'].'label__', - ], $options['entry_options']); + ], array_replace($options['entry_options'], $options['prototype_options'])); if (null !== $options['prototype_data']) { $prototypeOptions['data'] = $options['prototype_data']; @@ -120,12 +120,15 @@ public function configureOptions(OptionsResolver $resolver) 'prototype_name' => '__name__', 'entry_type' => TextType::class, 'entry_options' => [], + 'prototype_options' => [], 'delete_empty' => false, 'invalid_message' => 'The collection is invalid.', ]); $resolver->setNormalizer('entry_options', $entryOptionsNormalizer); + $resolver->setAllowedTypes('delete_empty', ['bool', 'callable']); + $resolver->setAllowedTypes('prototype_options', 'array'); } /** diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php index b7e68ba0d08d8..129c2c97b9a34 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php @@ -430,6 +430,23 @@ public function testPrototypeNotOverrideRequiredByEntryOptionsInFavorOfParent() $this->assertFalse($child->createView()->vars['prototype']->vars['required'], '"Prototype" should not be required'); } + public function testPrototypeOptionsOverrideEntryOptions() + { + $form = $this->factory->create(static::TESTED_TYPE, [], [ + 'allow_add' => true, + 'prototype' => true, + 'entry_type' => TextTypeTest::TESTED_TYPE, + 'entry_options' => [ + 'help' => null, + ], + 'prototype_options' => [ + 'help' => 'foo', + ], + ]); + + $this->assertSame('foo', $form->createView()->vars['prototype']->vars['help']); + } + public function testEntriesBlockPrefixes() { $collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [