Skip to content

Commit

Permalink
[Form] CollectionType apply prototypeOptions to ResizeFormListener ne…
Browse files Browse the repository at this point in the history
…w fields
  • Loading branch information
Thorry84 authored and nicolas-grekas committed Mar 31, 2023
1 parent efb8c3d commit 751efa8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Extension/Core/EventListener/ResizeFormListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion Extension/Core/Type/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions Tests/Extension/Core/Type/CollectionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, [''], [
Expand Down

0 comments on commit 751efa8

Please sign in to comment.