retrieve form error in front office

This commit is contained in:
Manuel Raynaud
2013-07-01 11:10:52 +02:00
parent a14d3da90a
commit 3b5b29c6e0
8 changed files with 58 additions and 18 deletions

View File

@@ -47,6 +47,8 @@ abstract class ActionEvent extends Event
*/
protected $action;
protected $form;
/**
*
* @param \Symfony\Component\HttpFoundation\Request $request
@@ -75,4 +77,21 @@ abstract class ActionEvent extends Event
{
return $this->request;
}
public function setFormError($form)
{
$this->form = $form;
$this->stopPropagation();
}
public function getForm()
{
return $this->form;
}
public function hasFormError()
{
return $this->form !== null;
}
}

View File

@@ -46,7 +46,14 @@ class ControllerListener implements EventSubscriberInterface
if (false !== $action = $request->get("action")) {
//search corresponding action
$event = new ActionEventFactory($request, $action, $event->getKernel()->getContainer()->getParameter("thelia.actionEvent"));
$dispatcher->dispatch("action.".$action, $event->createActionEvent());
$actionEvent = $event->createActionEvent();
$dispatcher->dispatch("action.".$action, $actionEvent);
if ($actionEvent->hasFormError()) {
$request->getSession()->set("form_error", true);
$request->getSession()->set("form_name", $actionEvent->getForm()->createView()
->vars["attr"]["thelia_name"]);
}
}
}

View File

@@ -88,8 +88,14 @@ class Form implements SmartyPluginInterface
}
$instance = $this->getInstance($params['name']);
$form = $instance->getForm();
$template->assign("form", $instance->getForm()->createView());
if (true === $this->request->getSession()->get("form_error", false) && $this->request->getSession()->get
("form_name") == $instance->getName()) {
$form->bind($this->request);
}
$template->assign("form", $form->createView());
} else {
return $content;
}
@@ -202,7 +208,10 @@ class Form implements SmartyPluginInterface
$class = new \ReflectionClass($this->formDefinition[$name]);
return $class->newInstance($this->request);
return $class->newInstance(
$this->request,
"form"
);
}
/**