From 00b87858b13d8418f7aea42cd0a86de2d80cc87c Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Mon, 11 Jul 2016 11:23:00 -0400 Subject: [PATCH 1/2] [WebProfilerBundle][Form] expand forms that contains children with errors --- .../Resources/views/Collector/form.html.twig | 2 +- .../DataCollector/FormDataCollector.php | 6 ++ .../DataCollector/FormDataCollectorTest.php | 58 +++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig index d69e42ab40f41..b018b49ea158b 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig @@ -443,7 +443,7 @@ {% if data.children is not empty %} {% endif %} diff --git a/src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php b/src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php index a408dde2306c9..e458f4f0e5d60 100644 --- a/src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php +++ b/src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php @@ -155,6 +155,12 @@ public function collectSubmittedData(FormInterface $form) foreach ($form as $child) { $this->collectSubmittedData($child); + + // Expand current form if there are children expanded or with errors + if (empty($this->dataByForm[$hash]['expanded'])) { + $childData = $this->dataByForm[spl_object_hash($child)]; + $this->dataByForm[$hash]['expanded'] = !empty($childData['expanded']) || !empty($childData['errors']); + } } } diff --git a/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php b/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php index f6fa2493d55c7..e7cb98e88417e 100644 --- a/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php @@ -123,6 +123,7 @@ public function testBuildPreliminaryFormTree() 'config' => 'foo', 'default_data' => 'foo', 'submitted_data' => 'foo', + 'expanded' => false, 'children' => array( 'child' => $childFormData, ), @@ -323,6 +324,7 @@ public function testBuildFinalFormTree() 'config' => 'foo', 'default_data' => 'foo', 'submitted_data' => 'foo', + 'expanded' => false, 'children' => array( 'child' => $childFormData, ), @@ -528,6 +530,62 @@ public function testCollectSubmittedDataCountsErrors() $this->assertSame(4, $data['nb_errors']); } + public function testCollectSubmittedDataExpandedFormsErrors() + { + $child1Form = $this->createForm('child1'); + $child11Form = $this->createForm('child11'); + $child2Form = $this->createForm('child2'); + $child21Form = $this->createForm('child21'); + + $child1Form->add($child11Form); + $child2Form->add($child21Form); + $this->form->add($child1Form); + $this->form->add($child2Form); + + $this->dataExtractor + ->method('extractConfiguration') + ->will($this->returnValue(array())); + $this->dataExtractor + ->method('extractDefaultData') + ->will($this->returnValue(array())); + $this->dataExtractor->expects($this->at(10)) + ->method('extractSubmittedData') + ->with($this->form) + ->will($this->returnValue(array('errors' => array()))); + $this->dataExtractor->expects($this->at(11)) + ->method('extractSubmittedData') + ->with($child1Form) + ->will($this->returnValue(array('errors' => array()))); + $this->dataExtractor->expects($this->at(12)) + ->method('extractSubmittedData') + ->with($child11Form) + ->will($this->returnValue(array('errors' => array('foo')))); + $this->dataExtractor->expects($this->at(13)) + ->method('extractSubmittedData') + ->with($child2Form) + ->will($this->returnValue(array('errors' => array()))); + $this->dataExtractor->expects($this->at(14)) + ->method('extractSubmittedData') + ->with($child21Form) + ->will($this->returnValue(array('errors' => array()))); + + $this->dataCollector->collectSubmittedData($this->form); + $this->dataCollector->buildPreliminaryFormTree($this->form); + + $data = $this->dataCollector->getData(); + $formData = $data['forms']['name']; + $child1Data = $formData['children']['child1']; + $child11Data = $child1Data['children']['child11']; + $child2Data = $formData['children']['child2']; + $child21Data = $child2Data['children']['child21']; + + $this->assertTrue($formData['expanded']); + $this->assertTrue($child1Data['expanded']); + $this->assertFalse(isset($child11Data['expanded']), 'The leaf data does not contains "expanded" property.'); + $this->assertFalse($child2Data['expanded']); + $this->assertFalse(isset($child21Data['expanded']), 'The leaf data does not contains "expanded" property.'); + } + private function createForm($name) { $builder = new FormBuilder($name, null, $this->dispatcher, $this->factory); From a85adae9c0874cd9624218f02511d82e6ae5d55b Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Mon, 25 Jul 2016 23:40:31 -0400 Subject: [PATCH 2/2] highlighting full error path --- .../Resources/views/Collector/form.html.twig | 16 +++++++++++----- .../DataCollector/FormDataCollector.php | 6 +++--- .../DataCollector/FormDataCollectorTest.php | 14 +++++++------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig index b018b49ea158b..c3e207bdc821f 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig @@ -164,6 +164,9 @@ font-weight: bold; vertical-align: middle; } + .has-error { + color: #B0413E; + } .errors h3 { color: #B0413E; } @@ -423,11 +426,12 @@ {% endblock %} -{% macro form_tree_entry(name, data, expanded) %} +{% macro form_tree_entry(name, data, is_root) %} {% import _self as tree %} + {% set has_error = data.errors is defined and data.errors|length > 0 %}
  • - {% if data.errors is defined and data.errors|length > 0 %} + {% if has_error %}
    {{ data.errors|length }}
    {% endif %} @@ -437,13 +441,15 @@
    {% endif %} - {{ name|default('(no name)') }} {% if data.type_class is defined %}[{{ data.type_class|split('\\')|last }}]{% endif %} + + {{ name|default('(no name)') }} {% if data.type_class is defined %}[{{ data.type_class|split('\\')|last }}]{% endif %} +
    {% if data.children is not empty %} -
      +
        {% for childName, childData in data.children %} - {{ tree.form_tree_entry(childName, childData, childData.expanded|default(false)) }} + {{ tree.form_tree_entry(childName, childData, false) }} {% endfor %}
      {% endif %} diff --git a/src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php b/src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php index e458f4f0e5d60..ec817c4e2c0ae 100644 --- a/src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php +++ b/src/Symfony/Component/Form/Extension/DataCollector/FormDataCollector.php @@ -156,10 +156,10 @@ public function collectSubmittedData(FormInterface $form) foreach ($form as $child) { $this->collectSubmittedData($child); - // Expand current form if there are children expanded or with errors - if (empty($this->dataByForm[$hash]['expanded'])) { + // Expand current form if there are children with errors + if (empty($this->dataByForm[$hash]['has_children_error'])) { $childData = $this->dataByForm[spl_object_hash($child)]; - $this->dataByForm[$hash]['expanded'] = !empty($childData['expanded']) || !empty($childData['errors']); + $this->dataByForm[$hash]['has_children_error'] = !empty($childData['has_children_error']) || !empty($childData['errors']); } } } diff --git a/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php b/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php index e7cb98e88417e..0fddf14dc57bc 100644 --- a/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/DataCollector/FormDataCollectorTest.php @@ -123,7 +123,7 @@ public function testBuildPreliminaryFormTree() 'config' => 'foo', 'default_data' => 'foo', 'submitted_data' => 'foo', - 'expanded' => false, + 'has_children_error' => false, 'children' => array( 'child' => $childFormData, ), @@ -324,7 +324,7 @@ public function testBuildFinalFormTree() 'config' => 'foo', 'default_data' => 'foo', 'submitted_data' => 'foo', - 'expanded' => false, + 'has_children_error' => false, 'children' => array( 'child' => $childFormData, ), @@ -579,11 +579,11 @@ public function testCollectSubmittedDataExpandedFormsErrors() $child2Data = $formData['children']['child2']; $child21Data = $child2Data['children']['child21']; - $this->assertTrue($formData['expanded']); - $this->assertTrue($child1Data['expanded']); - $this->assertFalse(isset($child11Data['expanded']), 'The leaf data does not contains "expanded" property.'); - $this->assertFalse($child2Data['expanded']); - $this->assertFalse(isset($child21Data['expanded']), 'The leaf data does not contains "expanded" property.'); + $this->assertTrue($formData['has_children_error']); + $this->assertTrue($child1Data['has_children_error']); + $this->assertFalse(isset($child11Data['has_children_error']), 'The leaf data does not contains "has_children_error" property.'); + $this->assertFalse($child2Data['has_children_error']); + $this->assertFalse(isset($child21Data['has_children_error']), 'The leaf data does not contains "has_children_error" property.'); } private function createForm($name)