Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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