retrieve form error in front office
This commit is contained in:
@@ -46,7 +46,7 @@ class Customer implements EventSubscriberInterface
|
||||
if ($form->isValid()) {
|
||||
echo "ok"; exit;
|
||||
} else {
|
||||
echo "ko"; exit;
|
||||
$event->setFormError($form);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,16 +79,4 @@ class BaseAdminController extends ContainerAware
|
||||
|
||||
return $parser;
|
||||
}
|
||||
|
||||
public function getFormFactory()
|
||||
{
|
||||
return BaseForm::getFormFactory($this->getRequest(), ConfigQuery::read("form.secret.admin", md5(__DIR__)));
|
||||
}
|
||||
|
||||
public function getFormBuilder()
|
||||
{
|
||||
return $this->getFormFactory()->createBuilder("form");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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"
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -30,6 +30,7 @@ use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension;
|
||||
use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
|
||||
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider;
|
||||
use Symfony\Component\Validator\Validation;
|
||||
use Thelia\Form\Extension\NameFormExtension;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
|
||||
abstract class BaseForm {
|
||||
@@ -38,17 +39,23 @@ abstract class BaseForm {
|
||||
*/
|
||||
protected $form;
|
||||
|
||||
public $name;
|
||||
|
||||
public function __construct(Request $request, $type= "form", $data = array(), $options = array())
|
||||
{
|
||||
$validator = Validation::createValidator();
|
||||
|
||||
if(!isset($options["attr"]["name"])) {
|
||||
$options["attr"]["thelia_name"] = $this->getName();
|
||||
}
|
||||
|
||||
$this->form = Forms::createFormFactoryBuilder()
|
||||
->addExtension(new HttpFoundationExtension())
|
||||
->addExtension(
|
||||
new CsrfExtension(
|
||||
new SessionCsrfProvider(
|
||||
$request->getSession(),
|
||||
isset($option["secret"]) ? $option["secret"] : ConfigQuery::read("form.secret", md5(__DIR__))
|
||||
isset($options["secret"]) ? $options["secret"] : ConfigQuery::read("form.secret", md5(__DIR__))
|
||||
)
|
||||
)
|
||||
)
|
||||
@@ -57,6 +64,8 @@ abstract class BaseForm {
|
||||
->createBuilder($type, $data, $options);
|
||||
;
|
||||
|
||||
|
||||
|
||||
$this->buildForm();
|
||||
}
|
||||
|
||||
@@ -69,6 +78,6 @@ abstract class BaseForm {
|
||||
}
|
||||
|
||||
abstract protected function buildForm();
|
||||
|
||||
abstract public function getName();
|
||||
}
|
||||
|
||||
|
||||
@@ -45,4 +45,9 @@ class CustomerCreation extends BaseForm
|
||||
)
|
||||
->add('age', 'integer');
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return "customerCreation";
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,9 @@ An image from asset directory :
|
||||
|
||||
{form_field_hidden form=$form}
|
||||
{form_field form=$form.email}
|
||||
{form_error form=$form.email}
|
||||
{$message}
|
||||
{/form_error}
|
||||
{intl l="{$label}"} : <input type="text" name="{$name}" {$attr} >
|
||||
{/form_field}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user