8000 [Form] deprecate 'prototype_data' option in favor of 'prototype_optio… · HeahDude/symfony@c969206 · GitHub
[go: up one dir, main page]

Skip to content

Commit c969206

Browse files
committed
[Form] deprecate 'prototype_data' option in favor of 'prototype_options' in CollectionType
1 parent 11e1aa8 commit c969206

File tree

2 files changed

+66
-18
lines changed

2 files changed

+66
-18
lines changed

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

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,7 @@ class CollectionType extends AbstractType
2727
public function buildForm(FormBuilderInterface $builder, array $options)
2828
{
2929
if ($options['allow_add'] && $options['prototype']) {
30-
$prototypeOptions = array_replace(array(
31-
'required' => $options['required'],
32-
'label' => $options['prototype_name'].'label__',
33-
), $options['entry_options']);
34-
35-
if (null !== $options['prototype_data']) {
36-
$prototypeOptions['data'] = $options['prototype_data'];
37-
}
38-
39-
$prototype = $builder->create($options['prototype_name'], $options['entry_type'], $prototypeOptions);
30+
$prototype = $builder->create($options['prototype_name'], $options['entry_type'], $options['prototype_options']);
4031
$builder->setAttribute('prototype', $prototype->getForm());
4132
}
4233

@@ -81,24 +72,53 @@ public function finishView(FormView $view, FormInterface $form, array $options)
8172
*/
8273
public function configureOptions(OptionsResolver $resolver)
8374
{
84-
$entryOptionsNormalizer = function (Options $options, $value) {
85-
$value['block_name'] = 'entry';
75+
$entryOptionsNormalizer = function (Options $options, $entryOptions) {
76+
$entryOptions['block_name'] = 'entry';
8677

87-
return $value;
78+
return $entryOptions;
79+
};
80+
81+
$prototypeOptionsNormalizer = function (Options $options, $prototypeOptions) {
82+
// Default to 'entry_options' option
83+
$prototypeOptions = array_replace($options['entry_options'], $prototypeOptions);
84+
85+
if (null !== $options['prototype_data']) {
86+
@trigger_error('The "prototype_data" option is deprecated since version 3.1 and will be removed in 4.0. You should use "prototype_options" option instead.', E_USER_DEPRECATED);
87+
88+
// BC, Let 'prototype_data' option override `$entryOptions['data']`
89+
$prototypeOptions['data'] = $options['prototype_data'];
90+
}
91+
92+
return array_replace(array(
93+
// Use the collection required state
94+
'required' => $options['required'],
95+
'label' => $options['prototype_name'].'label__',
96+
), $prototypeOptions);
8897
};
8998

9099
$resolver->setDefaults(array(
91-
'allow_add' => false,
92-
'allow_delete' => false,
93-
'prototype' => true,
94-
'prototype_data' => null,
95-
'prototype_name' => '__name__',
96100
'entry_type' => __NAMESPACE__.'\TextType',
97101
'entry_options' => array(),
102+
'prototype' => true,
103+
'prototype_data' => null, // deprecated
104+
'prototype_name' => '__name__',
105+
'prototype_options' => array(),
106+
'allow_add' => false,
107+
'allow_delete' => false,
98108
'delete_empty' => false,
99109
));
100110

101111
$resolver->setNormalizer('entry_options', $entryOptionsNormalizer);
112+
$resolver->setNormalizer('prototype_options', $prototypeOptionsNormalizer);
113+
114+
$resolver->setAllowedTypes('entry_type', array('string', 'Symfony\Component\Form\FormTypeInterface'));
115+
$resolver->setAllowedTypes('entry_options', 'array');
116+
$resolver->setAllowedTypes('prototype', 'bool');
117+
$resolver->setAllowedTypes('prototype_name', 'string');
118+
$resolver->setAllowedTypes('prototype_options', 'array');
119+
$resolver->setAllowedTypes('allow_add', 'bool');
120+
$resolver->setAllowedTypes('allow_delete', 'bool');
121+
$resolver->setAllowedTypes('delete_empty', 'bool');
102122
}
103123

104124
/**

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ public function testPrototypeDefaultLabel()
273273
$this->assertSame('__test__label__', $form->createView()->vars['prototype']->vars['label']);
274274
}
275275

276+
/**
277+
* @group legacy
278+
*/
276279
public function testPrototypeData()
277280
{
278281
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
@@ -290,6 +293,31 @@ public function testPrototypeData()
290293
$this->assertFalse($form->createView()->vars['prototype']->vars['label']);
291294
}
292295

296+
public function testPrototypeOptions()
297+
{
298+
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
299+
'allow_add' => true,
300+
'prototype' => true,
301+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
302+
'entry_options' => array(
303+
'data' => 'foo',
304+
'label' => 'Item:',
305+
'attr' => array('class' => 'my&item&class'),
306+
'label_attr' => array('class' => 'my&item&label&class'),
307+
),
308+
'prototype_options' => array(
309+
'data' => 'bar',
310+
'label' => false,
311+
'attr' => array('class' => 'my&prototype&class'),
312+
),
313+
));
314+
315+
$this->assertSame('bar', $form->createView()->vars['prototype']->vars['value']);
316+
$this->assertFalse($form->createView()->vars['prototype']->vars['label']);
317+
$this->assertSame('my&prototype&class', $form->createView()->vars['prototype']->vars['attr']['class']);
318+
$this->assertSame('my&item&label&class', $form->createView()->vars['prototype']->vars['label_attr']['class']);
319+
}
320+
293321
public function testPrototypeDefaultRequired()
294322
{
295323
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(

0 commit comments

Comments
 (0)
0