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
This commit is contained in:
commit
54bd6f76f8
@ -29,7 +29,9 @@ class CollectionType extends AbstractType
|
|||||||
if ($options['allow_add'] && $options['prototype']) {
|
if ($options['allow_add'] && $options['prototype']) {
|
||||||
$prototype = $builder->create($options['prototype_name'], $options['type'], array_replace(array(
|
$prototype = $builder->create($options['prototype_name'], $options['type'], array_replace(array(
|
||||||
'label' => $options['prototype_name'].'label__',
|
'label' => $options['prototype_name'].'label__',
|
||||||
), $options['options']));
|
), $options['options'], array(
|
||||||
|
'data' => $options['prototype_data'],
|
||||||
|
)));
|
||||||
$builder->setAttribute('prototype', $prototype->getForm());
|
$builder->setAttribute('prototype', $prototype->getForm());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,6 +86,7 @@ class CollectionType extends AbstractType
|
|||||||
'allow_add' => false,
|
'allow_add' => false,
|
||||||
'allow_delete' => false,
|
'allow_delete' => false,
|
||||||
'prototype' => true,
|
'prototype' => true,
|
||||||
|
'prototype_data' => null,
|
||||||
'prototype_name' => '__name__',
|
'prototype_name' => '__name__',
|
||||||
'type' => 'text',
|
'type' => 'text',
|
||||||
'options' => array(),
|
'options' => array(),
|
||||||
|
@ -274,4 +274,19 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
|
|||||||
|
|
||||||
$this->assertSame('__test__label__', $form->createView()->vars['prototype']->vars['label']);
|
$this->assertSame('__test__label__', $form->createView()->vars['prototype']->vars['label']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testPrototypeData()
|
||||||
|
{
|
||||||
|
$form = $this->factory->create('collection', array(), array(
|
||||||
|
'type' => 'text',
|
||||||
|
'allow_add' => true,
|
||||||
|
'prototype' => true,
|
||||||
|
'prototype_data' => 'foo',
|
||||||
|
'options' => array(
|
||||||
|
'data' => 'bar',
|
||||||
|
),
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertSame('foo', $form->createView()->vars['prototype']->vars['value']);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user