retrieve form error in front office
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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"]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user