Skip to content

Commit

Permalink
[Form] Fixed form debugger to work even when no view variables are lo…
Browse files Browse the repository at this point in the history
…gged (e.g. upon redirects)
  • Loading branch information
webmozart committed Oct 28, 2013
1 parent a9c17c3 commit 99a4b7e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
Expand Up @@ -173,7 +173,7 @@

{% macro form_tree_entry(name, data) %}
<li>
<a href="#details_{{ data.view_vars.id|default("") }}">{{ name }}</a>
<a href="#details_{{ data.id }}">{{ name }}</a>

{% if data.children is not empty %}
<ul>
Expand All @@ -186,7 +186,7 @@
{% endmacro %}

{% macro form_tree_details(name, data) %}
<div class="tree-details" id="details_{{ data.view_vars.id|default("") }}">
<div class="tree-details" id="details_{{ data.id }}">
<h2>
{{ name }}
{% if data.type_class is defined %}
Expand Down Expand Up @@ -323,6 +323,7 @@
</table>
{% endif %}

{% if data.view_vars is defined %}
<h3>View Variables</h3>

<table>
Expand All @@ -337,6 +338,7 @@
</tr>
{% endfor %}
</table>
{% endif %}
</div>

{% for childName, childData in data.children %}
Expand Down
Expand Up @@ -42,6 +42,7 @@ public function __construct(ValueExporter $valueExporter = null)
public function extractConfiguration(FormInterface $form)
{
$data = array(
'id' => $this->buildId($form),
'type' => $form->getConfig()->getType()->getName(),
'type_class' => get_class($form->getConfig()->getType()->getInnerType()),
'synchronized' => $this->valueExporter->exportValue($form->isSynchronized()),
Expand Down Expand Up @@ -132,4 +133,22 @@ public function extractViewVariables(FormView $view)

return $data;
}

/**
* Recursively builds an HTML ID for a form.
*
* @param FormInterface $form The form
*
* @return string The HTML ID
*/
private function buildId(FormInterface $form)
{
$id = $form->getName();

if (null !== $form->getParent()) {
$id = $this->buildId($form->getParent()).'_'.$id;
}

return $id;
}
}
Expand Up @@ -79,6 +79,7 @@ public function testExtractConfiguration()
->getForm();

$this->assertSame(array(
'id' => 'name',
'type' => 'type_name',
'type_class' => 'stdClass',
'synchronized' => 'true',
Expand Down Expand Up @@ -111,6 +112,7 @@ public function testExtractConfigurationSortsPassedOptions()
->getForm();

$this->assertSame(array(
'id' => 'name',
'type' => 'type_name',
'type_class' => 'stdClass',
'synchronized' => 'true',
Expand Down Expand Up @@ -144,6 +146,7 @@ public function testExtractConfigurationSortsResolvedOptions()
->getForm();

$this->assertSame(array(
'id' => 'name',
'type' => 'type_name',
'type_class' => 'stdClass',
'synchronized' => 'true',
Expand All @@ -156,6 +159,41 @@ public function testExtractConfigurationSortsResolvedOptions()
), $this->dataExtractor->extractConfiguration($form));
}

public function testExtractConfigurationBuildsIdRecursively()
{
$type = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
$type->expects($this->any())
->method('getName')
->will($this->returnValue('type_name'));
$type->expects($this->any())
->method('getInnerType')
->will($this->returnValue(new \stdClass()));

$grandParent = $this->createBuilder('grandParent')
->setCompound(true)
->setDataMapper($this->getMock('Symfony\Component\Form\DataMapperInterface'))
->getForm();
$parent = $this->createBuilder('parent')
->setCompound(true)
->setDataMapper($this->getMock('Symfony\Component\Form\DataMapperInterface'))
->getForm();
$form = $this->createBuilder('name')
->setType($type)
->getForm();

$grandParent->add($parent);
$parent->add($form);

$this->assertSame(array(
'id' => 'grandParent_parent_name',
'type' => 'type_name',
'type_class' => 'stdClass',
'synchronized' => 'true',
'passed_options' => array(),
'resolved_options' => array(),
), $this->dataExtractor->extractConfiguration($form));
}

public function testExtractDefaultData()
{
$form = $this->createBuilder('name')->getForm();
Expand Down

0 comments on commit 99a4b7e

Please sign in to comment.