From 3b5b29c6e0db27b9d0d5d95892807cdafae2c8d3 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Mon, 1 Jul 2013 11:10:52 +0200 Subject: [PATCH] retrieve form error in front office --- core/lib/Thelia/Action/Customer.php | 2 +- .../Admin/Controller/BaseAdminController.php | 12 ------------ core/lib/Thelia/Core/Event/ActionEvent.php | 19 +++++++++++++++++++ .../Core/EventListener/ControllerListener.php | 9 ++++++++- .../Core/Template/Smarty/Plugins/Form.php | 13 +++++++++++-- core/lib/Thelia/Form/BaseForm.php | 13 +++++++++++-- core/lib/Thelia/Form/CustomerCreation.php | 5 +++++ templates/smarty-sample/index.html | 3 +++ 8 files changed, 58 insertions(+), 18 deletions(-) diff --git a/core/lib/Thelia/Action/Customer.php b/core/lib/Thelia/Action/Customer.php index 186b8e34c..a794035e2 100755 --- a/core/lib/Thelia/Action/Customer.php +++ b/core/lib/Thelia/Action/Customer.php @@ -46,7 +46,7 @@ class Customer implements EventSubscriberInterface if ($form->isValid()) { echo "ok"; exit; } else { - echo "ko"; exit; + $event->setFormError($form); } } } diff --git a/core/lib/Thelia/Admin/Controller/BaseAdminController.php b/core/lib/Thelia/Admin/Controller/BaseAdminController.php index 3aa0e9281..1c193a966 100755 --- a/core/lib/Thelia/Admin/Controller/BaseAdminController.php +++ b/core/lib/Thelia/Admin/Controller/BaseAdminController.php @@ -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"); - } - - } \ No newline at end of file diff --git a/core/lib/Thelia/Core/Event/ActionEvent.php b/core/lib/Thelia/Core/Event/ActionEvent.php index b2d1d7b77..32074c559 100755 --- a/core/lib/Thelia/Core/Event/ActionEvent.php +++ b/core/lib/Thelia/Core/Event/ActionEvent.php @@ -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; + } + } diff --git a/core/lib/Thelia/Core/EventListener/ControllerListener.php b/core/lib/Thelia/Core/EventListener/ControllerListener.php index 607c2a44d..93c827c09 100755 --- a/core/lib/Thelia/Core/EventListener/ControllerListener.php +++ b/core/lib/Thelia/Core/EventListener/ControllerListener.php @@ -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"]); + } } } diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php index 51ff06e9c..0870ba2c2 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php @@ -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" + ); } /** diff --git a/core/lib/Thelia/Form/BaseForm.php b/core/lib/Thelia/Form/BaseForm.php index 02f86fe02..cbcebf173 100644 --- a/core/lib/Thelia/Form/BaseForm.php +++ b/core/lib/Thelia/Form/BaseForm.php @@ -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(); } diff --git a/core/lib/Thelia/Form/CustomerCreation.php b/core/lib/Thelia/Form/CustomerCreation.php index 68726356d..bcfacbf8a 100644 --- a/core/lib/Thelia/Form/CustomerCreation.php +++ b/core/lib/Thelia/Form/CustomerCreation.php @@ -45,4 +45,9 @@ class CustomerCreation extends BaseForm ) ->add('age', 'integer'); } + + public function getName() + { + return "customerCreation"; + } } \ No newline at end of file diff --git a/templates/smarty-sample/index.html b/templates/smarty-sample/index.html index 174505335..25814172e 100755 --- a/templates/smarty-sample/index.html +++ b/templates/smarty-sample/index.html @@ -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}"} : {/form_field}