Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 69e5e58

Browse files
committed
[Form] Individual rows of CollectionType cannot be styled anymore for performance reasons
1 parent cf41bf8 commit 69e5e58

File tree

10 files changed

+118
-16
lines changed

10 files changed

+118
-16
lines changed

UPGRADE-2.1.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,30 @@
576576
{% endblock %}
577577
````
578578
579+
* Custom styling of individual rows of a collection form has been removed for
580+
performance reasons. Instead, all rows now have the same block name, where
581+
the word "entry" replaces the previous occurence of the row index.
582+
583+
Before:
584+
585+
```
586+
{% block _author_tags_0_label %}
587+
{# ... #}
588+
{% endblock %}
589+
590+
{% block _author_tags_1_label %}
591+
{# ... #}
592+
{% endblock %}
593+
```
594+
595+
After:
596+
597+
```
598+
{% block _author_tags_entry_label %}
599+
{# ... #}
600+
{% endblock %}
601+
```
602+
579603
#### Other BC Breaks
580604
581605
* The order of the first two arguments of the methods `createNamed` and

src/Symfony/Bridge/Twig/Extension/FormExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ protected function render(FormView $view, $section, array $variables = array())
245245
$this->varStack[$rendering]['variables'] = array_replace_recursive($this->varStack[$rendering]['variables'], $variables);
246246
} else {
247247
$types = $view->getVar('types');
248-
$types[] = $custom;
248+
$types[] = $view->getVar('full_block_name');
249249
$typeIndex = count($types) - 1;
250250
$this->varStack[$rendering] = array(
251251
'variables' => array_replace_recursive($view->getVars(), $variables),
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
{% block _text_id_widget %}
2-
<div id="container">
3-
{{ form_widget(form) }}
4-
</div>
2+
{% spaceless %}
3+
<div id="container">
4+
{{ form_widget(form) }}
5+
</div>
6+
{% endspaceless %}
57
{% endblock _text_id_widget %}
8+
9+
{% block _name_entry_label %}
10+
{% spaceless %}
11+
{% if label is empty %}
12+
{% set label = name|humanize %}
13+
{% endif %}
14+
<label>Custom label: {{ label|trans({}, translation_domain) }}</label>
15+
{% endspaceless %}
16+
{% endblock _name_entry_label %}

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ protected function renderSection(FormView $view, $section, array $variables = ar
246246
$variables = array_replace_recursive($this->varStack[$rendering]['variables'], $variables);
247247
} else {
248248
$types = $view->getVar('types');
249-
$types[] = $custom;
249+
$types[] = $view->getVar('full_block_name');
250250
$typeIndex = count($types) - 1;
251251
$variables = array_replace_recursive($view->getVars(), $variables);
252252
$this->varStack[$rendering]['types'] = $types;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php if (!$label) { $label = $view['form']->humanize($name); } ?>
2+
<label>Custom label: <?php echo $view->escape($view['translator']->trans($label, array(), $translation_domain)) ?></label>

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,5 @@ CHANGELOG
170170
* `getExtensions`
171171
* `setExtensions`
172172
* ChoiceType now caches its created choice lists to improve performance
173+
* [BC BREAK] Rows of a collection field cannot be themed individually anymore. All rows in the collection
174+
field now have the same block names, which contains "entry" where it previously contained the row index.

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Form\FormViewInterface;
1717
use Symfony\Component\Form\FormInterface;
1818
use Symfony\Component\Form\Extension\Core\EventListener\ResizeFormListener;
19+
use Symfony\Component\OptionsResolver\Options;
1920
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
2021

2122
class CollectionType extends AbstractType
@@ -73,6 +74,12 @@ public function finishView(FormViewInterface $view, FormInterface $form, array $
7374
*/
7475
public function setDefaultOptions(OptionsResolverInterface $resolver)
7576
{
77+
$optionsFilter = function (Options $options, $value) {
78+
$value['block_name'] = 'entry';
79+
80+
return $value;
81+
};
82+
7683
$resolver->setDefaults(array(
7784
'allow_add' => false,
7885
'allow_delete' => false,
@@ -81,6 +88,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
8188
'type' => 'text',
8289
'options' => array(),
8390
));
91+
92+
$resolver->setFilters(array(
93+
'options' => $optionsFilter,
94+
));
8495
}
8596

8697
/**

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5959
public function buildView(FormViewInterface $view, FormInterface $form, array $options)
6060
{
6161
$name = $form->getName();
62+
$blockName = $options['block_name'] ?: $form->getName();
6263
$readOnly = $options['read_only'];
6364
$translationDomain = $options['translation_domain'];
6465

@@ -67,23 +68,30 @@ public function buildView(FormViewInterface $view, FormInterface $form, array $o
6768
throw new FormException('Form node with empty name can be used only as root form node.');
6869
}
6970

70-
if ('' !== ($parentFullName = $view->getParent()->getVar('full_name'))) {
71-
$id = sprintf('%s_%s', $view->getParent()->getVar('id'), $name);
71+
$parentView = $view->getParent();
72+
73+
if ('' !== ($parentFullName = $parentView->getVar('full_name'))) {
74+
$id = sprintf('%s_%s', $parentView->getVar('id'), $name);
7275
$fullName = sprintf('%s[%s]', $parentFullName, $name);
76+
$fullBlockName = sprintf('%s_%s', $parentView->getVar('full_block_name'), $blockName);
7377
} else {
7478
$id = $name;
7579
$fullName = $name;
80+
$fullBlockName = '_' . $blockName;
7681
}
7782

78-
// Complex fields are read-only if themselves or their parent is.
79-
$readOnly = $readOnly || $view->getParent()->getVar('read_only');
83+
// Complex fields are read-only if they themselves or their parents are.
84+
if (!$readOnly) {
85+
$readOnly = $parentView->getVar('read_only');
86+
}
8087

8188
if (!$translationDomain) {
82-
$translationDomain = $view->getParent()->getVar('translation_domain');
89+
$translationDomain = $parentView->getVar('translation_domain');
8390
}
8491
} else {
8592
$id = $name;
8693
$fullName = $name;
94+
$fullBlockName = '_' . $blockName;
8795

8896
// Strip leading underscores and digits. These are allowed in
8997
// form names, but not in HTML4 ID attributes.
@@ -105,6 +113,7 @@ public function buildView(FormViewInterface $view, FormInterface $form, array $o
105113
'id' => $id,
106114
'name' => $name,
107115
'full_name' => $fullName,
116+
'full_block_name' => $fullBlockName,
108117
'read_only' => $readOnly,
109118
'errors' => $form->getErrors(),
110119
'valid' => $form->isBound() ? $form->isValid() : true,
@@ -177,7 +186,14 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
177186
return false !== $options['property_path'];
178187
};
179188

189+
// If data is given, the form is locked to that data
190+
// (independent of its value)
191+
$resolver->setOptional(array(
192+
'data',
193+
));
194+
180195
$resolver->setDefaults(array(
196+
'block_name' => null,
181197
'data_class' => $dataClass,
182198
'empty_data' => $emptyData,
183199
'trim' => true,
@@ -198,12 +214,6 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
198214
'translation_domain' => null,
199215
));
200216

201-
// If data is given, the form is locked to that data
202-
// (independent of its value)
203-
$resolver->setOptional(array(
204-
'data',
205-
));
206-
207217
$resolver->setAllowedTypes(array(
208218
'attr' => 'array',
209219
'label_attr' => 'array',

src/Symfony/Component/Form/Tests/AbstractDivLayoutTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,27 @@ public function testThemeInheritance($parentTheme, $childTheme)
557557
]
558558
/following-sibling::input[@type="hidden"]
559559
]
560+
'
561+
);
562+
}
563+
564+
/**
565+
* The block "_name_child_label" should be overridden in the theme of the
566+
* implemented driver.
567+
*/
568+
public function testCollectionRowWithCustomBlock()
569+
{
570+
$collection = array('one', 'two', 'three');
571+
$form = $this->factory->createNamedBuilder('name', 'collection', $collection)
572+
->getForm();
573+
574+
$this->assertWidgetMatchesXpath($form->createView(), array(),
575+
'/div
576+
[
577+
./div[./label[.="Custom label: [trans]0[/trans]"]]
578+
/following-sibling::div[./label[.="Custom label: [trans]1[/trans]"]]
579+
/following-sibling::div[./label[.="Custom label: [trans]2[/trans]"]]
580+
]
560581
'
561582
);
562583
}

src/Symfony/Component/Form/Tests/AbstractTableLayoutTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,27 @@ public function testRepeatedWithCustomOptions()
338338
]
339339
]
340340
[count(.//input)=3]
341+
'
342+
);
343+
}
344+
345+
/**
346+
* The block "_name_child_label" should be overridden in the theme of the
347+
* implemented driver.
348+
*/
349+
public function testCollectionRowWithCustomBlock()
350+
{
351+
$collection = array('one', 'two', 'three');
352+
$form = $this->factory->createNamedBuilder('name', 'collection', $collection)
353+
->getForm();
354+
355+
$this->assertWidgetMatchesXpath($form->createView(), array(),
356+
'/table
357+
[
358+
./tr[./td/label[.="Custom label: [trans]0[/trans]"]]
359+
/following-sibling::tr[./td/label[.="Custom label: [trans]1[/trans]"]]
360+
/following-sibling::tr[./td/label[.="Custom label: [trans]2[/trans]"]]
361+
]
341362
'
342363
);
343364
}

0 commit comments

Comments
 (0)