diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form_debug.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form_debug.xml
index a03ea495b5..b0c0e3ff65 100644
--- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/form_debug.xml
+++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/form_debug.xml
@@ -6,16 +6,12 @@
Symfony\Component\Form\Extension\DataCollector\Type\DataCollectorTypeExtension
- Symfony\Component\Form\Extension\DataCollector\EventListener\DataCollectorSubscriber
-
-
-
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 7ee053d189..ab9db1f28e 100644
--- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig
+++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig
@@ -1,13 +1,13 @@
-{% extends app.request.isXmlHttpRequest ? 'WebProfilerBundle:Profiler:ajax_layout.html.twig' : 'WebProfilerBundle:Profiler:layout.html.twig' %}
+{% extends '@WebProfiler/Profiler/layout.html.twig' %}
{% block toolbar %}
- {% if collector.dataCount %}
+ {% if collector.data|length %}
{% set icon %}
- {{ collector.dataCount }}
+ {% if collector.errorCount %}{{ collector.errorCount }}{% else %}{{ collector.data|length }}{% endif %}
{% endset %}
- {% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %}
+ {% include '@WebProfiler/Profiler/toolbar_item.html.twig' with { 'link': profiler_url } %}
{% endif %}
{% endblock %}
@@ -15,17 +15,18 @@
Forms
- {% if collector.dataCount %}
- {{ collector.dataCount }}
- {% endif %}
+ {% if collector.data|length %}
+ {{ collector.data|length }}
+ {% endif %}
{% endblock %}
{% block panel %}
-
Invalid Forms
+ Form{% if collector.data|length > 1 %}s{% endif %}
{% for formName, fields in collector.data %}
{{ formName }}
+ {% if fields %}
Field |
@@ -50,7 +51,10 @@
{% endfor %}
+ {% else %}
+ This form is valid.
+ {% endif %}
{% else %}
- No invalid form{% if collector.dataCount > 1 %}s{% endif %} detected for this request.
+ No forms were submitted for this request.
{% endfor %}
{% endblock %}
diff --git a/src/Symfony/Component/Form/Extension/DataCollector/Collector/FormCollector.php b/src/Symfony/Component/Form/Extension/DataCollector/Collector/FormCollector.php
index 5cb0080df7..82564e4068 100644
--- a/src/Symfony/Component/Form/Extension/DataCollector/Collector/FormCollector.php
+++ b/src/Symfony/Component/Form/Extension/DataCollector/Collector/FormCollector.php
@@ -11,18 +11,29 @@
namespace Symfony\Component\Form\Extension\DataCollector\Collector;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Symfony\Component\Form\FormEvent;
+use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector as BaseCollector;
/**
- * DataCollector for Form Validation Failures.
+ * DataCollector for Form Validation.
*
* @author Robert Schönthal
*/
-class FormCollector extends BaseCollector
+class FormCollector extends BaseCollector implements EventSubscriberInterface
{
+ /**
+ * {@inheritDoc}
+ */
+ public static function getSubscribedEvents()
+ {
+ return array(FormEvents::POST_SUBMIT => array('collectForm', -255));
+ }
+
/**
* {@inheritDoc}
*/
@@ -31,16 +42,48 @@ class FormCollector extends BaseCollector
//nothing to do, everything is added with addError()
}
+ /**
+ * Collects Form-Validation-Data and adds them to the Collector.
+ *
+ * @param FormEvent $event The event object
+ */
+ public function collectForm(FormEvent $event)
+ {
+ $form = $event->getForm();
+
+ if ($form->isRoot()) {
+ $this->data[$form->getName()] = array();
+ $this->addForm($form);
+ }
+ }
+
+ /**
+ * Adds an Form-Element to the Collector.
+ *
+ * @param FormInterface $form
+ */
+ private function addForm(FormInterface $form)
+ {
+ if ($form->getErrors()) {
+ $this->addError($form);
+ }
+
+ // recursively add all child-errors
+ foreach ($form->all() as $field) {
+ $this->addForm($field);
+ }
+ }
+
/**
* Adds a Form-Error to the Collector.
*
* @param FormInterface $form
*/
- public function addError(FormInterface $form)
+ private function addError(FormInterface $form)
{
$storeData = array(
'root' => $form->getRoot()->getName(),
- 'name' => (string)$form->getPropertyPath(),
+ 'name' => (string) $form->getPropertyPath(),
'type' => $form->getConfig()->getType()->getName(),
'errors' => $form->getErrors(),
'value' => $this->varToString($form->getViewData())
@@ -68,12 +111,20 @@ class FormCollector extends BaseCollector
}
/**
- * Returns the number of invalid Forms.
+ * Returns the number of Forms with Errors.
*
* @return integer
*/
- public function getDataCount()
+ public function getErrorCount()
{
- return count($this->data);
+ $errorCount = 0;
+
+ foreach ($this->data as $form) {
+ if (count($form)) {
+ $errorCount++;
+ }
+ }
+
+ return $errorCount;
}
}
diff --git a/src/Symfony/Component/Form/Extension/DataCollector/DataCollectorExtension.php b/src/Symfony/Component/Form/Extension/DataCollector/DataCollectorExtension.php
index c35a1a0b74..f9f8209188 100644
--- a/src/Symfony/Component/Form/Extension/DataCollector/DataCollectorExtension.php
+++ b/src/Symfony/Component/Form/Extension/DataCollector/DataCollectorExtension.php
@@ -40,4 +40,4 @@ class DataCollectorExtension extends AbstractExtension
new Type\DataCollectorTypeExtension($this->eventSubscriber)
);
}
-}
+}
diff --git a/src/Symfony/Component/Form/Extension/DataCollector/EventListener/DataCollectorSubscriber.php b/src/Symfony/Component/Form/Extension/DataCollector/EventListener/DataCollectorSubscriber.php
deleted file mode 100644
index be343eed1f..0000000000
--- a/src/Symfony/Component/Form/Extension/DataCollector/EventListener/DataCollectorSubscriber.php
+++ /dev/null
@@ -1,75 +0,0 @@
-
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Component\Form\Extension\DataCollector\EventListener;
-
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-use Symfony\Component\Form\Extension\DataCollector\Collector\FormCollector;
-use Symfony\Component\Form\FormEvent;
-use Symfony\Component\Form\FormEvents;
-use Symfony\Component\Form\FormInterface;
-
-/**
- * EventSubscriber for adding Form Validation Failures to the DataCollector.
- *
- * @author Robert Schönthal
- */
-class DataCollectorSubscriber implements EventSubscriberInterface
-{
- /**
- * @var FormCollector
- */
- private $collector;
-
- public function __construct(FormCollector $collector)
- {
- $this->collector = $collector;
- }
-
- /**
- * {@inheritDoc}
- */
- public static function getSubscribedEvents()
- {
- return array(FormEvents::POST_SUBMIT => array('addToProfiler', -255));
- }
-
- /**
- * Searches for invalid Form-Data and adds them to the Collector.
- *
- * @param FormEvent $event The event object
- */
- public function addToProfiler(FormEvent $event)
- {
- $form = $event->getForm();
-
- if ($form->isRoot()) {
- $this->addErrors($form);
- }
- }
-
- /**
- * Adds an invalid Form-Element to the Collector.
- *
- * @param FormInterface $form
- */
- private function addErrors(FormInterface $form)
- {
- if ($form->getErrors()) {
- $this->collector->addError($form);
- }
-
- //recursively add all child errors
- foreach ($form->all() as $field) {
- $this->addErrors($field);
- }
- }
-}
diff --git a/src/Symfony/Component/Form/Tests/Extension/DataCollector/Collector/FormCollectorTest.php b/src/Symfony/Component/Form/Tests/Extension/DataCollector/Collector/FormCollectorTest.php
index 99b585d888..20bf9878df 100644
--- a/src/Symfony/Component/Form/Tests/Extension/DataCollector/Collector/FormCollectorTest.php
+++ b/src/Symfony/Component/Form/Tests/Extension/DataCollector/Collector/FormCollectorTest.php
@@ -11,15 +11,23 @@
namespace Symfony\Component\Form\Tests\Extension\DataCollector\Collector;
use Symfony\Component\Form\Extension\DataCollector\Collector\FormCollector;
+use Symfony\Component\Form\FormEvent;
+use Symfony\Component\Form\FormEvents;
-/**
- * @covers Symfony\Component\Form\Extension\DataCollector\Collector\FormCollector
- */
class FormCollectorTest extends \PHPUnit_Framework_TestCase
{
- public function testAddError()
+ public function testSubscribedEvents()
+ {
+ $events = FormCollector::getSubscribedEvents();
+
+ $this->assertInternalType('array', $events);
+ $this->assertEquals(array(FormEvents::POST_SUBMIT => array('collectForm', -255)), $events);
+ }
+
+ public function testCollect()
{
$form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
+ $subForm = $this->getMock('Symfony\Component\Form\Test\FormInterface');
$type = $this->getMock('Symfony\Component\Form\FormTypeInterface');
$type->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('fizz'));
@@ -27,19 +35,23 @@ class FormCollectorTest extends \PHPUnit_Framework_TestCase
$config = $this->getMock('Symfony\Component\Form\FormConfigInterface');
$config->expects($this->atLeastOnce())->method('getType')->will($this->returnValue($type));
- $form->expects($this->atLeastOnce())->method('getRoot')->will($this->returnSelf());
- $form->expects($this->atLeastOnce())->method('getPropertyPath')->will($this->returnValue('bar'));
+ $form->expects($this->atLeastOnce())->method('all')->will($this->returnValue(array($subForm)));
+ $form->expects($this->atLeastOnce())->method('isRoot')->will($this->returnValue(true));
$form->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('foo'));
- $form->expects($this->atLeastOnce())->method('getViewData')->will($this->returnValue('bazz'));
- $form->expects($this->atLeastOnce())->method('getErrors')->will($this->returnValue(array('foo')));
- $form->expects($this->atLeastOnce())->method('getConfig')->will($this->returnValue($config));
+ $subForm->expects($this->atLeastOnce())->method('all')->will($this->returnValue(array()));
+ $subForm->expects($this->atLeastOnce())->method('getErrors')->will($this->returnValue(array('foo')));
+ $subForm->expects($this->atLeastOnce())->method('getRoot')->will($this->returnValue($form));
+ $subForm->expects($this->atLeastOnce())->method('getConfig')->will($this->returnValue($config));
+ $subForm->expects($this->atLeastOnce())->method('getPropertyPath')->will($this->returnValue('bar'));
+ $subForm->expects($this->atLeastOnce())->method('getViewData')->will($this->returnValue('bazz'));
+ $event = new FormEvent($form, array());
$c = new FormCollector();
- $c->addError($form);
+ $c->collectForm($event);
$this->assertInternalType('array', $c->getData());
- $this->assertEquals(1, $c->getDataCount());
+ $this->assertEquals(1, $c->getErrorCount());
$this->assertEquals(array('foo' => array('bar' => array('value' => 'bazz', 'root' => 'foo', 'type' => 'fizz', 'name' => 'bar', 'errors' => array('foo')))), $c->getData());
}
}
diff --git a/src/Symfony/Component/Form/Tests/Extension/DataCollector/EventListener/DataCollectorSubscriberTest.php b/src/Symfony/Component/Form/Tests/Extension/DataCollector/EventListener/DataCollectorSubscriberTest.php
deleted file mode 100644
index 9bd8cdf6da..0000000000
--- a/src/Symfony/Component/Form/Tests/Extension/DataCollector/EventListener/DataCollectorSubscriberTest.php
+++ /dev/null
@@ -1,71 +0,0 @@
-
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
-
-namespace Symfony\Component\Form\Tests\Extension\DataCollector\EventListener;
-
-use Symfony\Component\Form\Extension\DataCollector\Collector\FormCollector;
-use Symfony\Component\Form\Extension\DataCollector\EventListener\DataCollectorSubscriber;
-use Symfony\Component\Form\FormEvent;
-use Symfony\Component\Form\FormEvents;
-
-/**
- * @covers Symfony\Component\Form\Extension\DataCollector\EventListener\DataCollectorSubscriber
- */
-class DataCollectorSubscriberTest extends \PHPUnit_Framework_TestCase
-{
- /**
- * @var DataCollectorSubscriber
- */
- private $eventSubscriber;
-
- /**
- * @var FormCollector
- */
- private $collector;
-
- public function setUp()
- {
- $this->collector = $this->getMockBuilder('Symfony\Component\Form\Extension\DataCollector\Collector\FormCollector')->setMethods(array('addError'))->getMock();
- $this->eventSubscriber = new DataCollectorSubscriber($this->collector);
- }
-
- public function testSubscribedEvents()
- {
- $events = DataCollectorSubscriber::getSubscribedEvents();
-
- $this->assertInternalType('array', $events);
- $this->assertEquals(array(FormEvents::POST_SUBMIT => array('addToProfiler', -255)), $events);
- }
-
- public function testAddToProfilerWithSubForm()
- {
- $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
- $form->expects($this->atLeastOnce())->method('isRoot')->will($this->returnValue(false));
-
- $formEvent = new FormEvent($form, array());
-
- $this->collector->expects($this->never())->method('addError');
- $this->eventSubscriber->addToProfiler($formEvent);
- }
-
- public function testAddToProfilerWithInValidForm()
- {
- $form = $this->getMock('Symfony\Component\Form\Test\FormInterface');
- $form->expects($this->atLeastOnce())->method('isRoot')->will($this->returnValue(true));
- $form->expects($this->atLeastOnce())->method('getErrors')->will($this->returnValue(array('foo')));
- $form->expects($this->once())->method('all')->will($this->returnValue(array()));
-
- $formEvent = new FormEvent($form, array());
-
- $this->collector->expects($this->atLeastOnce())->method('addError')->with($form);
- $this->eventSubscriber->addToProfiler($formEvent);
- }
-}
-
\ No newline at end of file