diff --git a/Extension/Core/EventListener/ResizeFormListener.php b/Extension/Core/EventListener/ResizeFormListener.php index a524e1557..f68d5b6d2 100644 --- a/Extension/Core/EventListener/ResizeFormListener.php +++ b/Extension/Core/EventListener/ResizeFormListener.php @@ -26,18 +26,20 @@ class ResizeFormListener implements EventSubscriberInterface { protected $type; protected $options; + protected $prototypeOptions; protected $allowAdd; protected $allowDelete; private \Closure|bool $deleteEmpty; - public function __construct(string $type, array $options = [], bool $allowAdd = false, bool $allowDelete = false, bool|callable $deleteEmpty = false) + public function __construct(string $type, array $options = [], bool $allowAdd = false, bool $allowDelete = false, bool|callable $deleteEmpty = false, array $prototypeOptions = null) { $this->type = $type; $this->allowAdd = $allowAdd; $this->allowDelete = $allowDelete; $this->options = $options; $this->deleteEmpty = \is_bool($deleteEmpty) ? $deleteEmpty : $deleteEmpty(...); + $this->prototypeOptions = $prototypeOptions ?? $options; } public static function getSubscribedEvents(): array @@ -96,7 +98,7 @@ public function preSubmit(FormEvent $event) if (!$form->has($name)) { $form->add($name, $this->type, array_replace([ 'property_path' => '['.$name.']', - ], $this->options)); + ], $this->prototypeOptions)); } } } diff --git a/Extension/Core/Type/CollectionType.php b/Extension/Core/Type/CollectionType.php index c310441cc..f93c36abb 100644 --- a/Extension/Core/Type/CollectionType.php +++ b/Extension/Core/Type/CollectionType.php @@ -23,6 +23,7 @@ class CollectionType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { + $prototypeOptions = null; if ($options['allow_add'] && $options['prototype']) { $prototypeOptions = array_replace([ 'required' => $options['required'], @@ -42,7 +43,8 @@ public function buildForm(FormBuilderInterface $builder, array $options) $options['entry_options'], $options['allow_add'], $options['allow_delete'], - $options['delete_empty'] + $options['delete_empty'], + $prototypeOptions ); $builder->addEventSubscriber($resizeListener); diff --git a/Tests/Extension/Core/Type/CollectionTypeTest.php b/Tests/Extension/Core/Type/CollectionTypeTest.php index c7e10307b..816261104 100644 --- a/Tests/Extension/Core/Type/CollectionTypeTest.php +++ b/Tests/Extension/Core/Type/CollectionTypeTest.php @@ -447,6 +447,29 @@ public function testPrototypeOptionsOverrideEntryOptions() $this->assertSame('foo', $form->createView()->vars['prototype']->vars['help']); } + public function testPrototypeOptionsAppliedToNewFields() + { + $form = $this->factory->create(static::TESTED_TYPE, ['first'], [ + 'allow_add' => true, + 'prototype' => true, + 'entry_type' => TextTypeTest::TESTED_TYPE, + 'entry_options' => [ + 'disabled' => true, + ], + 'prototype_options' => [ + 'disabled' => false, + ], + ]); + + $form->submit(['first_changed', 'second']); + + $this->assertTrue($form->has('0')); + $this->assertTrue($form->has('1')); + $this->assertSame('first', $form[0]->getData()); + $this->assertSame('second', $form[1]->getData()); + $this->assertSame(['first', 'second'], $form->getData()); + } + public function testEntriesBlockPrefixes() { $collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [