8000 bug #49835 [Form] CollectionType apply prototypeOptions to ResizeForm… · symfony/symfony@6b9538b · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b9538b

Browse files
bug #49835 [Form] CollectionType apply prototypeOptions to ResizeFormListener new fields (Thorry84)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- [Form] CollectionType apply prototypeOptions to ResizeFormListener new fields Fixes issue 49786 | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #49786 | License | MIT This pull request provides a fix for issue 49786. By passing the prototypeOptions to the ResizeFormListener from the CollectionType and applying it to new fields it makes the prototypeOptions function as intended. Otherwise the use case such as in this blog aren't possible: https://symfony.com/blog/new-in-symfony-6-1-customizable-collection-prototypes I've updated the existing ResizeFormListener tests to handle the new signature and added a new test to the CollectionType which would fail without this fix and pass with the fix. Commits ------- 85167da [Form] CollectionType apply prototypeOptions to ResizeFormListener new fields
2 parents bf65d7b + 85167da commit 6b9538b

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/Symfony/Component/Form/Extension/Core/EventListener/ResizeFormListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@ class ResizeFormListener implements EventSubscriberInterface
2626
{
2727
protected $type;
2828
protected $options;
29+
protected $prototypeOptions;
2930
protected $allowAdd;
3031
protected $allowDelete;
3132

3233
private \Closure|bool $deleteEmpty;
3334

34-
public function __construct(string $type, array $options = [], bool $allowAdd = false, bool $allowDelete = false, bool|callable $deleteEmpty = false)
35+
public function __construct(string $type, array $options = [], bool $allowAdd = false, bool $allowDelete = false, bool|callable $deleteEmpty = false, array $prototypeOptions = null)
3536
{
3637
$this->type = $type;
3738
$this->allowAdd = $allowAdd;
3839
$this->allowDelete = $allowDelete;
3940
$this->options = $options;
4041
$this->deleteEmpty = \is_bool($deleteEmpty) ? $deleteEmpty : $deleteEmpty(...);
42+
$this->prototypeOptions = $prototypeOptions ?? $options;
4143
}
4244

4345
public static function getSubscribedEvents(): array
@@ -96,7 +98,7 @@ public function preSubmit(FormEvent $event)
9698
if (!$form->has($name)) {
9799
$form->add($name, $this->type, array_replace([
98100
'property_path' => '['.$name.']',
99-
], $this->options));
101+
], $this->prototypeOptions));
100102
}
101103
}
102104
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class CollectionType extends AbstractType
2323
{
2424
public function buildForm(FormBuilderInterface $builder, array $options)
2525
{
26+
$prototypeOptions = null;
2627
if ($options['allow_add'] && $options['prototype']) {
2728
$prototypeOptions = array_replace([
2829
'required' => $options['required'],
@@ -42,7 +43,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4243
$options['entry_options'],
4344
$options['allow_add'],
4445
$options['allow_delete'],
45-
$options['delete_empty']
46+
$options['delete_empty'],
47+
$prototypeOptions
4648
);
4749

4850
$builder->addEventSubscriber($resizeListener);

src/Symfony/Component/Form/Tests/Extension/Core/Type/CollectionTypeTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,29 @@ public function testPrototypeOptionsOverrideEntryOptions()
447447
$this->assertSame('foo', $form->createView()->vars['prototype']->vars['help']);
448448
}
449449

450+
public function testPrototypeOptionsAppliedToNewFields()
451+
{
452+
$form = $this->factory->create(static::TESTED_TYPE, ['first'], [
453+
'allow_add' => true,
454+
'prototype' => true,
455+
'entry_type' => TextTypeTest::TESTED_TYPE,
456+
'entry_options' => [
457+
'disabled' => true,
458+
],
459+
'prototype_options' => [
460+
'disabled' => false,
461+
],
462+
]);
463+
464+
$form->submit(['first_changed', 'second']);
465+
466+
$this->assertTrue($form->has('0'));
467+
$this->assertTrue($form->has('1'));
468+
$this->assertSame('first', $form[0]->getData());
469+
$this->assertSame('second', $form[1]->getData());
470+
$this->assertSame(['first', 'second'], $form->getData());
471+
}
472+
450473
public function testEntriesBlockPrefixes()
451474
{
452475
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [

0 commit comments

Comments
 (0)
0