In an Action, errored forms are propagated via the ActionEvent

This commit is contained in:
franck
2013-07-24 20:08:00 +02:00
parent 94ee77f4f8
commit 182b13ceca
8 changed files with 40 additions and 23 deletions

View File

@@ -39,7 +39,6 @@ use Thelia\Model\ConfigQuery;
use Thelia\Tools\Redirect; use Thelia\Tools\Redirect;
use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Component\Validator\Exception\ValidatorException;
use Thelia\Core\Security\Exception\AuthenticationException; use Thelia\Core\Security\Exception\AuthenticationException;
use Thelia\Core\Template\ParserContext;
use Thelia\Core\Security\Exception\UsernameNotFoundException; use Thelia\Core\Security\Exception\UsernameNotFoundException;
use Propel\Runtime\Exception\PropelException; use Propel\Runtime\Exception\PropelException;
@@ -51,14 +50,8 @@ class Customer implements EventSubscriberInterface
*/ */
protected $securityContext; protected $securityContext;
/** public function __construct(SecurityContext $securityContext) {
* @var Thelia\Core\Template\ParserContext
*/
protected $parserContext;
public function __construct(SecurityContext $securityContext, ParserContext $parserContext) {
$this->securityContext = $securityContext; $this->securityContext = $securityContext;
$this->parserContext = $parserContext;
} }
public function create(ActionEvent $event) public function create(ActionEvent $event)
@@ -119,7 +112,7 @@ class Customer implements EventSubscriberInterface
$customerCreationForm->setErrorMessage($message); $customerCreationForm->setErrorMessage($message);
// Store the form in the parser context // Store the form in the parser context
$this->parserContext->setErrorForm($customerCreationForm); $event->setErrorForm($customerCreationForm);
// Stop event propagation // Stop event propagation
$event->stopPropagation(); $event->stopPropagation();
@@ -183,11 +176,8 @@ class Customer implements EventSubscriberInterface
$customerModification->setError(true); $customerModification->setError(true);
$customerModification->setErrorMessage($message); $customerModification->setErrorMessage($message);
// Store the form in the parser context // Dispatch the errored form
$this->parserContext->setErrorForm($customerModification); $event->setErrorForm($customerModification);
// Stop event propagation
$event->stopPropagation();
} }
@@ -241,8 +231,8 @@ class Customer implements EventSubscriberInterface
$customerLoginForm->setError(true); $customerLoginForm->setError(true);
$customerLoginForm->setErrorMessage($message); $customerLoginForm->setErrorMessage($message);
// Store the form name in session (see Form Smarty plugin to find usage of this parameter) // Dispatch the errored form
$this->parserContext->setErrorForm($customerLoginForm); $event->setErrorForm($customerLoginForm);
// A this point, the same view is displayed again. // A this point, the same view is displayed again.
} }

View File

@@ -22,7 +22,6 @@
<tag name="kernel.event_subscriber"/> <tag name="kernel.event_subscriber"/>
<argument type="service" id="thelia.securityContext"/> <argument type="service" id="thelia.securityContext"/>
<argument type="service" id="thelia.parser.context"/>
</service> </service>
</services> </services>

View File

@@ -34,8 +34,9 @@
<argument type="service" id="service_container" /> <argument type="service" id="service_container" />
</service> </service>
<service id="thelia.controller_listener" class="Thelia\Core\EventListener\ControllerListener"> <service id="thelia.controller_listener" class="Thelia\Core\EventListener\ControllerListener" scope="request">
<tag name="kernel.event_subscriber"/> <tag name="kernel.event_subscriber"/>
<argument type="service" id="thelia.parser.context"/>
</service> </service>
<!-- <!--

View File

@@ -49,7 +49,7 @@ abstract class ActionEvent extends Event
*/ */
protected $action; protected $action;
protected $form = null; protected $errorForm = null;
/** /**
* *
@@ -79,4 +79,18 @@ abstract class ActionEvent extends Event
{ {
return $this->request; return $this->request;
} }
public function setErrorForm(BaseForm $form) {
$this->errorForm = $form;
if ($form != null) $this->stopPropagation();
}
public function getErrorForm() {
return $this->errorForm;
}
public function hasErrorForm() {
return $this->errorForm != null ? true : false;
}
} }

View File

@@ -26,6 +26,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Thelia\Core\Factory\ActionEventFactory; use Thelia\Core\Factory\ActionEventFactory;
use Thelia\Core\Template\ParserContext;
/** /**
* *
@@ -38,6 +39,14 @@ use Thelia\Core\Factory\ActionEventFactory;
*/ */
class ControllerListener implements EventSubscriberInterface class ControllerListener implements EventSubscriberInterface
{ {
/**
* @var ParserContext the parser context
*/
protected $parserContext;
public function __construct(ParserContext $parserContext) {
$this->parserContext = $parserContext;
}
public function onKernelController(FilterControllerEvent $event) public function onKernelController(FilterControllerEvent $event)
{ {
@@ -49,6 +58,11 @@ class ControllerListener implements EventSubscriberInterface
$event = new ActionEventFactory($request, $action, $event->getKernel()->getContainer()->getParameter("thelia.actionEvent")); $event = new ActionEventFactory($request, $action, $event->getKernel()->getContainer()->getParameter("thelia.actionEvent"));
$actionEvent = $event->createActionEvent(); $actionEvent = $event->createActionEvent();
$dispatcher->dispatch("action.".$action, $actionEvent); $dispatcher->dispatch("action.".$action, $actionEvent);
// Process form errors
if ($actionEvent->hasErrorForm()) {
$this->parserContext->setErrorForm($actionEvent->getErrorForm());
}
} }
} }

View File

@@ -27,7 +27,6 @@ use Symfony\Component\Validator\ExecutionContextInterface;
use Thelia\Model\ConfigQuery; use Thelia\Model\ConfigQuery;
use Thelia\Model\CustomerQuery; use Thelia\Model\CustomerQuery;
class CustomerCreation extends BaseForm class CustomerCreation extends BaseForm
{ {
@@ -161,6 +160,6 @@ class CustomerCreation extends BaseForm
public function getName() public function getName()
{ {
return "customerCreation"; return "thelia.customer.creation";
} }
} }

View File

@@ -51,6 +51,6 @@ class CustomerLogin extends BaseForm {
public function getName() public function getName()
{ {
return "customer_login"; return "thelia.customer.login";
} }
} }

View File

@@ -115,6 +115,6 @@ class CustomerModification extends BaseForm {
*/ */
public function getName() public function getName()
{ {
return "customerModification"; return "thelia.customer.modification";
} }
} }