Skip to content

Commit 54bd6f7

Browse files
committed
feature #12314 [Form] Add "prototype_data" option to collection type (kgilden)
This PR was submitted for the master branch but it was merged into the 2.8 branch instead (closes #12314). Discussion ---------- [Form] Add "prototype_data" option to collection type | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #5095 | License | MIT | Doc PR | symfony/symfony-docs#4367 Makes it possible to have default data for collection prototypes. Commits ------- 80b0a80 [Form] Add "prototype_data" option to collection type
2 parents 09fabfe + 80b0a80 commit 54bd6f7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
2929
if ($options['allow_add'] && $options['prototype']) {
3030
$prototype = $builder->create($options['prototype_name'], $options['type'], array_replace(array(
3131
'label' => $options['prototype_name'].'label__',
32-
), $options['options']));
32+
), $options['options'], array(
33+
'data' => $options['prototype_data'],
34+
)));
3335
$builder->setAttribute('prototype', $prototype->getForm());
3436
}
3537

@@ -84,6 +86,7 @@ public function configureOptions(OptionsResolver $resolver)
8486
'allow_add' => false,
8587
'allow_delete' => false,
8688
'prototype' => true,
89+
'prototype_data' => null,
8790
'prototype_name' => '__name__',
8891
'type' => 'text',
8992
'options' => array(),

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,19 @@ public function testPrototypeDefaultLabel()
274274

275275
$this->assertSame('__test__label__', $form->createView()->vars['prototype']->vars['label']);
276276
}
277+
278+
public function testPrototypeData()
279+
{
280+
$form = $this->factory->create('collection', array(), array(
281+
'type' => 'text',
282+
'allow_add' => true,
283+
'prototype' => true,
284+
'prototype_data' => 'foo',
285+
'options' => array(
286+
'data' => 'bar',
287+
),
288+
));
289+
290+
$this->assertSame('foo', $form->createView()->vars['prototype']->vars['value']);
291+
}
277292
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy