Skip to content

Commit b532dba

Browse files
committed
feature #15025 [2.8] [Form] Rename CollectionType options for entries (WouterJ)
This PR was merged into the 2.8 branch. Discussion ---------- [2.8] [Form] Rename CollectionType options for entries Description --- Replaces #13820 for the 2.8 branch. Original description: > `type` and `options` are extremely generic. Prefixing them with `entry_` makes it clear what they are configuring. > About the property deprecation it is the same story as symfony/symfony#13717 and I don't know which direction you want me to go. I've tried to apply the comments in the previous PR, but got a bit lost in the normalizers/default closure stuff. I hope I did everything correctly, but please review :) PR Info Table --- | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #7831 | License | MIT | Doc PR | symfony/symfony-docs#5051 Commits ------- 942a237 Rename CollectionType options for entries
2 parents fd7270f + 56fe2eb commit b532dba

File tree

4 files changed

+81
-34
lines changed

4 files changed

+81
-34
lines changed

Extension/Core/Type/CollectionType.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ class CollectionType extends AbstractType
2727
public function buildForm(FormBuilderInterface $builder, array $options)
2828
{
2929
if ($options['allow_add'] && $options['prototype']) {
30-
$prototype = $builder->create($options['prototype_name'], $options['type'], array_replace(array(
30+
$prototype = $builder->create($options['prototype_name'], $options['entry_type'], array_replace(array(
3131
'label' => $options['prototype_name'].'label__',
32-
), $options['options'], array(
32+
), $options['entry_options'], array(
3333
'data' => $options['prototype_data'],
3434
)));
3535
$builder->setAttribute('prototype', $prototype->getForm());
3636
}
3737

3838
$resizeListener = new ResizeFormListener(
39-
$options['type'],
40-
$options['options'],
39+
$options['entry_type'],
40+
$options['entry_options'],
4141
$options['allow_add'],
4242
$options['allow_delete'],
4343
$options['delete_empty']
@@ -76,24 +76,55 @@ public function finishView(FormView $view, FormInterface $form, array $options)
7676
*/
7777
public function configureOptions(OptionsResolver $resolver)
7878
{
79-
$optionsNormalizer = function (Options $options, $value) {
79+
$entryOptionsNormalizer = function (Options $options, $value) {
8080
$value['block_name'] = 'entry';
8181

8282
return $value;
8383
};
84+
$optionsNormalizer = function (Options $options, $value) use ($entryOptionsNormalizer) {
85+
if (null !== $value) {
86+
@trigger_error('The form option "options" is deprecated since version 2.8 and will be removed in 3.0. Use "entry_options" instead.', E_USER_DEPRECATED);
87+
}
88+
89+
return $entryOptionsNormalizer($options, $value);
90+
};
91+
$typeNormalizer = function (Options $options, $value) {
92+
if (null !== $value) {
93+
@trigger_error('The form option "type" is deprecated since version 2.8 and will be removed in 3.0. Use "entry_type" instead.', E_USER_DEPRECATED);
94+
}
95+
};
96+
$entryType = function (Options $options) {
97+
if (null !== $options['type']) {
98+
return $options['type'];
99+
}
100+
101+
return __NAMESPACE__.'\TextType';
102+
};
103+
$entryOptions = function (Options $options) {
104+
if (1 === count($options['options']) && isset($options['block_name'])) {
105+
return array();
106+
}
107+
108+
return $options['options'];
109+
};
84110

85111
$resolver->setDefaults(array(
86112
'allow_add' => false,
87113
'allow_delete' => false,
88114
'prototype' => true,
89115
'prototype_data' => null,
90116
'prototype_name' => '__name__',
91-
'type' => __NAMESPACE__.'\TextType',
92-
'options' => array(),
117+
// deprecated as of Symfony 2.8, to be removed in Symfony 3.0. Use entry_type instead
118+
'type' => null,
119+
// deprecated as of Symfony 2.8, to be removed in Symfony 3.0. Use entry_options instead
120+
'options' => null,
121+
'entry_type' => $entryType,
122+
'entry_options' => $entryOptions,
93123
'delete_empty' => false,
94124
));
95125

96126
$resolver->setNormalizer('options', $optionsNormalizer);
127+
$resolver->setNormalizer('entry_options', $entryOptionsNormalizer);
97128
}
98129

99130
/**

Tests/AbstractDivLayoutTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public function testRestAndRepeatedWithWidgetPerChild()
284284
public function testCollection()
285285
{
286286
$form = $this->factory->createNamed('names', 'Symfony\Component\Form\Extension\Core\Type\CollectionType', array('a', 'b'), array(
287-
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
287+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
288288
));
289289

290290
$this->assertWidgetMatchesXpath($form->createView(), array(),
@@ -306,7 +306,7 @@ public function testCollectionWithAlternatingRowTypes()
306306
array('title' => 'b'),
307307
);
308308
$form = $this->factory->createNamed('names', 'Symfony\Component\Form\Extension\Core\Type\CollectionType', $data, array(
309-
'type' => 'Symfony\Component\Form\Tests\Fixtures\AlternatingRowType',
309+
'entry_type' => 'Symfony\Component\Form\Tests\Fixtures\AlternatingRowType',
310310
));
311311

312312
$this->assertWidgetMatchesXpath($form->createView(), array(),
@@ -324,7 +324,7 @@ public function testCollectionWithAlternatingRowTypes()
324324
public function testEmptyCollection()
325325
{
326326
$form = $this->factory->createNamed('names', 'Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
327-
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
327+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
328328
));
329329

330330
$this->assertWidgetMatchesXpath($form->createView(), array(),
@@ -341,7 +341,7 @@ public function testCollectionRow()
341341
'collection',
342342
'Symfony\Component\Form\Extension\Core\Type\CollectionType',
343343
array('a', 'b'),
344-
array('type' => 'Symfony\Component\Form\Extension\Core\Type\TextType')
344+
array('entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType')
345345
);
346346

347347
$form = $this->factory->createNamedBuilder('form', 'Symfony\Component\Form\Extension\Core\Type\FormType')

Tests/Extension/Core/Type/CollectionTypeTest.php

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CollectionTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
2222
public function testLegacyName()
2323
{
2424
$form = $this->factory->create('collection', array(
25-
'type' => 'text',
25+
'entry_type' => 'text',
2626
));
2727

2828
$this->assertSame('collection', $form->getConfig()->getType()->getName());
@@ -40,8 +40,8 @@ public function testContainsNoChildByDefault()
4040
public function testSetDataAdjustsSize()
4141
{
4242
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
43-
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
44-
'options' => array(
43+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
44+
'entry_options' => array(
4545
'attr' => array('maxlength' => 20),
4646
),
4747
));
@@ -69,7 +69,7 @@ public function testSetDataAdjustsSize()
6969
public function testThrowsExceptionIfObjectIsNotTraversable()
7070
{
7171
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
72-
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
72+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
7373
));
7474
$this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
7575
$form->setData(new \stdClass());
@@ -78,7 +78,7 @@ public function testThrowsExceptionIfObjectIsNotTraversable()
7878
public function testNotResizedIfSubmittedWithMissingData()
7979
{
8080
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
81-
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
81+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
8282
));
8383
$form->setData(array('[email protected]', '[email protected]'));
8484
$form->submit(array('[email protected]'));
@@ -92,7 +92,7 @@ public function testNotResizedIfSubmittedWithMissingData()
9292
public function testResizedDownIfSubmittedWithMissingDataAndAllowDelete()
9393
{
9494
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
95-
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
95+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
9696
'allow_delete' => true,
9797
));
9898
$form->setData(array('[email protected]', '[email protected]'));
@@ -107,7 +107,7 @@ public function testResizedDownIfSubmittedWithMissingDataAndAllowDelete()
107107
public function testResizedDownIfSubmittedWithEmptyDataAndDeleteEmpty()
108108
{
109109
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
110-
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
110+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
111111
'allow_delete' => true,
112112
'delete_empty' => true,
113113
));
@@ -124,7 +124,7 @@ public function testResizedDownIfSubmittedWithEmptyDataAndDeleteEmpty()
124124
public function testDontAddEmptyDataIfDeleteEmpty()
125125
{
126126
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
127-
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
127+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
128128
'allow_add' => true,
129129
'delete_empty' => true,
130130
));
@@ -141,7 +141,7 @@ public function testDontAddEmptyDataIfDeleteEmpty()
141141
public function testNoDeleteEmptyIfDeleteNotAllowed()
142142
{
143143
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
144-
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
144+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
145145
'allow_delete' => false,
146146
'delete_empty' => true,
147147
));
@@ -156,10 +156,10 @@ public function testNoDeleteEmptyIfDeleteNotAllowed()
156156
public function testResizedDownIfSubmittedWithCompoundEmptyDataAndDeleteEmpty()
157157
{
158158
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
159-
'type' => 'Symfony\Component\Form\Tests\Fixtures\AuthorType',
159+
'entry_type' => 'Symfony\Component\Form\Tests\Fixtures\AuthorType',
160160
// If the field is not required, no new Author will be created if the
161161
// form is completely empty
162-
'options' => array('required' => false),
162+
'entry_options' => array('required' => false),
163163
'allow_add' => true,
164164
'delete_empty' => true,
165165
));
@@ -179,7 +179,7 @@ public function testResizedDownIfSubmittedWithCompoundEmptyDataAndDeleteEmpty()
179179
public function testNotResizedIfSubmittedWithExtraData()
180180
{
181181
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
182-
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
182+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
183183
));
184184
$form->setData(array('[email protected]'));
185185
$form->submit(array('[email protected]', '[email protected]'));
@@ -192,7 +192,7 @@ public function testNotResizedIfSubmittedWithExtraData()
192192
public function testResizedUpIfSubmittedWithExtraDataAndAllowAdd()
193193
{
194194
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
195-
'type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
195+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\TextType',
196196
'allow_add' => true,
197197
));
198198
$form->setData(array('[email protected]'));
@@ -208,7 +208,7 @@ public function testResizedUpIfSubmittedWithExtraDataAndAllowAdd()
208208
public function testAllowAddButNoPrototype()
209209
{
210210
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
211-
'type' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
211+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
212212
'allow_add' => true,
213213
'prototype' => false,
214214
));
@@ -220,7 +220,7 @@ public function testPrototypeMultipartPropagation()
220220
{
221221
$form = $this->factory
222222
->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
223-
'type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
223+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
224224
'allow_add' => true,
225225
'prototype' => true,
226226
))
@@ -232,7 +232,7 @@ public function testPrototypeMultipartPropagation()
232232
public function testGetDataDoesNotContainsPrototypeNameBeforeDataAreSet()
233233
{
234234
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
235-
'type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
235+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
236236
'prototype' => true,
237237
'allow_add' => true,
238238
));
@@ -244,7 +244,7 @@ public function testGetDataDoesNotContainsPrototypeNameBeforeDataAreSet()
244244
public function testGetDataDoesNotContainsPrototypeNameAfterDataAreSet()
245245
{
246246
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
247-
'type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
247+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
248248
'allow_add' => true,
249249
'prototype' => true,
250250
));
@@ -257,15 +257,15 @@ public function testGetDataDoesNotContainsPrototypeNameAfterDataAreSet()
257257
public function testPrototypeNameOption()
258258
{
259259
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
260-
'type' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
260+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
261261
'prototype' => true,
262262
'allow_add' => true,
263263
));
264264

265265
$this->assertSame('__name__', $form->getConfig()->getAttribute('prototype')->getName(), '__name__ is the default');
266266

267267
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
268-
'type' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
268+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FormType',
269269
'prototype' => true,
270270
'allow_add' => true,
271271
'prototype_name' => '__test__',
@@ -274,10 +274,26 @@ public function testPrototypeNameOption()
274274
$this->assertSame('__test__', $form->getConfig()->getAttribute('prototype')->getName());
275275
}
276276

277+
/**
278+
* @group legacy
279+
*/
280+
public function testLegacyEntryOptions()
281+
{
282+
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
283+
'type' => 'Symfony\Component\Form\Extension\Core\Type\NumberType',
284+
'options' => array('attr' => array('maxlength' => '10')),
285+
));
286+
287+
$resolvedOptions = $form->getConfig()->getOptions();
288+
289+
$this->assertEquals('Symfony\Component\Form\Extension\Core\Type\NumberType', $resolvedOptions['entry_type']);
290+
$this->assertEquals(array('attr' => array('maxlength' => '10'), 'block_name' => 'entry'), $resolvedOptions['entry_options']);
291+
}
292+
277293
public function testPrototypeDefaultLabel()
278294
{
279295
$form = $this->factory->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', array(), array(
280-
'type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
296+
'entry_type' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
281297
'allow_add' => true,
282298
'prototype' => true,
283299
'prototype_name' => '__test__',
@@ -293,7 +309,7 @@ public function testPrototypeData()
293309
'allow_add' => true,
294310
'prototype' => true,
295311
'prototype_data' => 'foo',
296-
'options' => array(
312+
'entry_options' => array(
297313
'data' => 'bar',
298314
),
299315
));

Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ public function testNoCsrfProtectionOnPrototype()
347347
{
348348
$prototypeView = $this->factory
349349
->create('Symfony\Component\Form\Extension\Core\Type\CollectionType', null, array(
350-
'type' => __CLASS__.'_ChildType',
351-
'options' => array(
350+
'entry_type' => __CLASS__.'_ChildType',
351+
'entry_options' => array(
352352
'csrf_field_name' => 'csrf',
353353
),
354354
'prototype' => true,

0 commit comments

Comments
 (0)