Skip to content

Commit 587fa43

Browse files
committed
[WebProfilerBundle][Form] expand forms that contains children with errors
1 parent 4dde7a5 commit 587fa43

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@
443443
{% if data.children is not empty %}
444444
<ul id="{{ data.id }}-children" {% if not expanded %}class="hidden"{% endif %}>
445445
{% for childName, childData in data.children %}
446-
{{ tree.form_tree_entry(childName, childData, false) }}
446+
{{ tree.form_tree_entry(childName, childData, childData.expanded|default(false)) }}
447447
{% endfor %}
448448
</ul>
449449
{% endif %}

src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ public function collectSubmittedData(FormInterface $form)
157157

158158
foreach ($form as $child) {
159159
$this->collectSubmittedData($child);
160+
161+
// Expand current form if there are children expanded or with errors
162+
if (empty($this->dataByForm[$hash]['expanded'])) {
163+
$childData = $this->dataByForm[spl_object_hash($child)];
164+
$this->dataByForm[$hash]['expanded'] = !empty($childData['expanded']) || !empty($childData['errors']);
165+
}
160166
}
161167
}
162168

src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public function testBuildPreliminaryFormTree()
123123
'config' => 'foo',
124124
'default_data' => 'foo',
125125
'submitted_data' => 'foo',
126+
'expanded' => false,
126127
'children' => array(
127128
'child' => $childFormData,
128129
),
@@ -323,6 +324,7 @@ public function testBuildFinalFormTree()
323324
'config' => 'foo',
324325
'default_data' => 'foo',
325326
'submitted_data' => 'foo',
327+
'expanded' => false,
326328
'children' => array(
327329
'child' => $childFormData,
328330
),
@@ -528,6 +530,62 @@ public function testCollectSubmittedDataCountsErrors()
528530
$this->assertSame(4, $data['nb_errors']);
529531
}
530532

533+
public function testCollectSubmittedDataExpandedFormsErrors()
534+
{
535+
$child1Form = $this->createForm('child1');
536+
$child11Form = $this->createForm('child11');
537+
$child2Form = $this->createForm('child2');
538+
$child21Form = $this->createForm('child21');
539+
540+
$child1Form->add($child11Form);
541+
$child2Form->add($child21Form);
542+
$this->form->add($child1Form);
543+
$this->form->add($child2Form);
544+
545+
$this->dataExtractor
546+
->method('extractConfiguration')
547+
->will($this->returnValue(array()));
548+
$this->dataExtractor
549+
->method('extractDefaultData')
550+
->will($this->returnValue(array()));
551+
$this->dataExtractor->expects($this->at(10))
552+
->method('extractSubmittedData')
553+
->with($this->form)
554+
->will($this->returnValue(array('errors' => array())));
555+
$this->dataExtractor->expects($this->at(11))
556+
->method('extractSubmittedData')
557+
->with($child1Form)
558+
->will($this->returnValue(array('errors' => array())));
559+
$this->dataExtractor->expects($this->at(12))
560+
->method('extractSubmittedData')
561+
->with($child11Form)
562+
->will($this->returnValue(array('errors' => array('foo'))));
563+
$this->dataExtractor->expects($this->at(13))
564+
->method('extractSubmittedData')
565+
->with($child2Form)
566+
->will($this->returnValue(array('errors' => array())));
567+
$this->dataExtractor->expects($this->at(14))
568+
->method('extractSubmittedData')
569+
->with($child21Form)
570+
->will($this->returnValue(array('errors' => array())));
571+
572+
$this->dataCollector->collectSubmittedData($this->form);
573+
$this->dataCollector->buildPreliminaryFormTree($this->form);
574+
575+
$data = $this->dataCollector->getData();
576+
$formData = $data['forms']['name'];
577+
$child1Data = $formData['children']['child1'];
578+
$child11Data = $child1Data['children']['child11'];
579+
$child2Data = $formData['children']['child2'];
580+
$child21Data = $child2Data['children']['child21'];
581+
582+
$this->assertTrue($formData['expanded']);
583+
$this->assertTrue($child1Data['expanded']);
584+
$this->assertFalse(isset($child11Data['expanded']), 'The leaf data does not contains "expanded" property.');
585+
$this->assertFalse($child2Data['expanded']);
586+
$this->assertFalse(isset($child21Data['expanded']), 'The leaf data does not contains "expanded" property.');
587+
}
588+
531589
private function createForm($name)
532590
{
533591
$builder = new FormBuilder($name, null, $this->dispatcher, $this->factory);

0 commit comments

Comments
 (0)