[Form] Merged subsriber/collector, also collect valid forms
This commit is contained in:
parent
1972a91653
commit
a994a5d410
@ -6,16 +6,12 @@
|
||||
|
||||
<parameters>
|
||||
<parameter key="form.type_extension.form.data_collector.class">Symfony\Component\Form\Extension\DataCollector\Type\DataCollectorTypeExtension</parameter>
|
||||
<parameter key="form.type_extension.form.data_collector.event_subscriber.class">Symfony\Component\Form\Extension\DataCollector\EventListener\DataCollectorSubscriber</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
<!-- DataCollectorExtension -->
|
||||
<service id="form.type_extension.form.data_collector" class="%form.type_extension.form.data_collector.class%">
|
||||
<tag name="form.type_extension" alias="form" />
|
||||
<argument type="service" id="form.type_extension.form.data_collector.event_subscriber" />
|
||||
</service>
|
||||
<service id="form.type_extension.form.data_collector.event_subscriber" class="%form.type_extension.form.data_collector.event_subscriber.class%" public="false">
|
||||
<argument type="service" id="data_collector.form" />
|
||||
</service>
|
||||
</services>
|
||||
|
@ -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 %}
|
||||
<img width="20" height="28" alt="Forms" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAbCAYAAAB836/YAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyRpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0N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KS1iDYSGJy9OA4Fe+EBKUsKWhlmwpMtZZCS/NHzJ5iFFCRuN7wXEEtQ0sJDirIfGrwBiLiL0HSbWwLPoWQkH2I0tu+KKlOVk+BSngclALEnNMHSldqTMJtOcCFwGNlDbhZ1Y1LwhwpwTuAz0waI4gxIXPqC2lzuoYeAPalajjIO+ogcIMAAaCkonoOPIhQAAAABJRU5ErkJggg=="/>
|
||||
<span class="sf-toolbar-status{% if collector.dataCount %} sf-toolbar-status-red{% endif %}">{{ collector.dataCount }}</span>
|
||||
<span class="sf-toolbar-status sf-toolbar-status-{% if collector.errorCount %}red{% else %}green{% endif %}">{% if collector.errorCount %}{{ collector.errorCount }}{% else %}{{ collector.data|length }}{% endif %}</span>
|
||||
{% 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 @@
|
||||
<span class="label">
|
||||
<span class="icon"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAbCAYAAAB836/YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAKT2lDQ1BQaG90b3Nob3AgSUNDIHByb2ZpbGUAAHjanVNnVFPpFj333vRCS4iAlEtvUhUIIFJCi4AUkSYqIQkQSoghodkVUcERRUUEG8igiAOOjoCMFVEsDIoK2AfkIaKOg6OIisr74Xuja9a89+bN/rXXPues852zzwfACAyWSDNRNYAMqUIeEeCDx8TG4eQuQIEKJHAAEAizZCFz/SMBAPh+PDwrIsAHvgABeNMLCADATZvAMByH/w/qQplcAYCEAcB0kThLCIAUAEB6jkKmAEBGAYCdmCZTAKAEAGDLY2LjAFAtAGAnf+bTAICd+Jl7AQBblCEVAaCRACATZYhEAGg7AKzPVopFAFgwABRmS8Q5ANgtADBJV2ZIALC3AMDOEAuyAAgMADBRiIUpAAR7A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6mF+KMfTjvnnKBgYD+heaQUJjlnRFJVunvpwd0lykyoqIRFXhWoqgSwvkgEwGV2my+CctYgaMumlXu0A6aUmsNHs1oaZiatQDM7BXL4ynelB550cFgDcmRD6eTwsaP0NmQrkP+Vv3vlv+G1hrwDfN8HOxw4DwC6ITLy7UIfRQAAAABJRU5ErkJggg==" alt=""/></span>
|
||||
<strong>Forms</strong>
|
||||
{% if collector.dataCount %}
|
||||
<span class="count"><span>{{ collector.dataCount }}</span></span>
|
||||
{% if collector.data|length %}
|
||||
<span class="count"><span>{{ collector.data|length }}</span></span>
|
||||
{% endif %}
|
||||
</span>
|
||||
{% endblock %}
|
||||
|
||||
{% block panel %}
|
||||
<h2>Invalid Forms</h2>
|
||||
<h2>Form{% if collector.data|length > 1 %}s{% endif %}</h2>
|
||||
|
||||
{% for formName, fields in collector.data %}
|
||||
<h3>{{ formName }}</h3>
|
||||
{% if fields %}
|
||||
<table>
|
||||
<tr>
|
||||
<th>Field</th>
|
||||
@ -51,6 +52,9 @@
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% else %}
|
||||
<em>No invalid form{% if collector.dataCount > 1 %}s{% endif %} detected for this request.</em>
|
||||
<em>This form is valid.</em>
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<em>No forms were submitted for this request.</em>
|
||||
{% endfor %}
|
||||
{% endblock %}
|
||||
|
@ -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 <robert.schoenthal@gmail.com>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -1,75 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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 <robert.schoenthal@gmail.com>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -1,71 +0,0 @@
|
||||
<?php
|
||||
/*
|
||||
* This file is part of the Symfony package.
|
||||
*
|
||||
* (c) Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user