10000 feature #19339 [WebProfilerBundle][Form][DX] To expand the form nodes… · symfony/symfony@d64bcda · GitHub
[go: up one dir, main page]

Skip to content

Commit d64bcda

Browse files
committed
feature #19339 [WebProfilerBundle][Form][DX] To expand the form nodes that contains children with errors (yceruto)
This PR was squashed before being merged into the 3.2-dev branch (closes #19339). Discussion ---------- [WebProfilerBundle][Form][DX] To expand the form nodes that contains children with errors | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Currently when we use nested forms and an error occurs into one of them, it's not displayed "easily" in the form panel profiler: ![first](https://cloud.githubusercontent.com/assets/2028198/17125622/1fd15142-52c3-11e6-830e-17b3e341ba60.png) This happen because only the root form is expanded and the children are shown collapsed "by default". **The main problem is to search where is the form with error**. The purpose of this PR is to show expanded all forms that contains children with error, reducing a little bit the developer's time when debugging. PR result when we access to the form panel profiler: ![form-error-result](https://cloud.githubusercontent.com/assets/2028198/17125447/83eb9c0c-52c1-11e6-94bc-a2a7492eea43.png) In red the full path to the error. ![form-error-result2](https://cloud.githubusercontent.com/assets/2028198/17125459/a04de95e-52c1-11e6-8980-84a5dcd0914a.png) Commits ------- d626b28 [WebProfilerBundle][Form][DX] To expand the form nodes that contains children with errors
2 parents 703db1e + d626b28 commit d64bcda

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@
164164
font-weight: bold;
165165
vertical-align: middle;
166166
}
167+
.has-error {
168+
color: #B0413E;
169+
}
167170
.errors h3 {
168171
color: #B0413E;
169172
}
@@ -423,11 +426,12 @@
423426
</script>
424427
{% endblock %}
425428

426-
{% macro form_tree_entry(name, data, expanded) %}
429+
{% macro form_tree_entry(name, data, is_root) %}
427430
{% import _self as tree %}
431+
{% set has_error = data.errors is defined and data.errors|length > 0 %}
428432
<li>
429433
<div class="tree-inner" data-tab-target-id="{{ data.id }}-details">
430-
{% if data.errors is defined and data.errors|length > 0 %}
434+
{% if has_error %}
431435
<div class="badge-error">{{ data.errors|length }}</div>
432436
{% endif %}
433437

@@ -437,11 +441,13 @@
437441
<div class="toggle-icon empty"></div>
438442
{% endif %}
439443

440-
{{ name|default('(no name)') }} {% if data.type_class is defined %}[<abbr title="{{ data.type_class }}">{{ data.type_class|split('\\')|last }}</abbr>]{% endif %}
444+
<span {% if has_error or data.has_children_error|default(false) %}class="has-error"{% endif %}>
445+
{{ name|default('(no name)') }} {% if data.type_class is defined %}[<abbr title="{{ data.type_class }}">{{ data.type_class|split('\\')|last }}</abbr>]{% endif %}
446+
</span>
441447
</div>
442448

443449
{% if data.children is not empty %}
444-
<ul id="{{ data.id }}-children" {% if not expanded %}class="hidden"{% endif %}>
450+
<ul id="{{ data.id }}-children" {% if not is_root and not data.has_children_error|default(false) %}class="hidden"{% endif %}>
445451
{% for childName, childData in data.children %}
446452
{{ tree.form_tree_entry(childName, childData, false) }}
447453
{% endfor %}

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

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

156156
foreach ($form as $child) {
157157
$this->collectSubmittedData($child);
158+
159+
// Expand current form if there are children with errors
160+
if (empty($this->dataByForm[$hash]['has_children_error'])) {
161+
$childData = $this->dataByForm[spl_object_hash($child)];
162+
$this->dataByForm[$hash]['has_children_error'] = !empty($childData['has_children_error']) || !empty($childData['errors']);
163+
}
158164
}
159165
}
160166

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+
'has_children_error' => 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+
'has_children_error' => 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['has_children_error']);
583+
$this->assertTrue($child1Data['has_children_error']);
584+
$this->assertFalse(isset($child11Data['has_children_error']), 'The leaf data does not contains "has_children_error" property.');
585+
$this->assertFalse($child2Data['has_children_error']);
586+
$this->assertFalse(isset($child21Data['has_children_error']), 'The leaf data does not contains "has_children_error" property.');
587+
}
588+
531589
private function createForm($name)
532590
{
533591
$builder = new FormBuilder($name, null, $this->dispatcher, $this->factory);

0 commit comments

Comments
 (0)
0