From 8dd43d6779c340f7c97534698b70e9e30aa7df8a Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 25 Jun 2013 16:54:42 +0200 Subject: [PATCH] add form_error block --- .../Admin/Controller/AdminController.php | 20 +++++++------ .../Thelia/Config/Resources/routing/admin.xml | 3 ++ .../Core/Template/Smarty/Plugins/Form.php | 30 ++++++++++++++++++- templates/admin/default/login.html | 3 ++ 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/core/lib/Thelia/Admin/Controller/AdminController.php b/core/lib/Thelia/Admin/Controller/AdminController.php index 6965858eb..39cdfb4fc 100755 --- a/core/lib/Thelia/Admin/Controller/AdminController.php +++ b/core/lib/Thelia/Admin/Controller/AdminController.php @@ -30,11 +30,7 @@ class AdminController extends BaseAdminController { public function indexAction() { - $form = $this->getFormBuilder(); - - $adminLogin = new AdminLogin(); - - $form = $adminLogin->buildForm($form, array())->getForm(); + $form = $this->getLoginForm(); $request = $this->getRequest(); @@ -42,10 +38,7 @@ class AdminController extends BaseAdminController { $form->bind($request); if($form->isValid()) { - //TODO - - } else { - //TODO + echo "valid"; exit; } } @@ -53,4 +46,13 @@ class AdminController extends BaseAdminController { "form" => $form->createView() )); } + + protected function getLoginForm() + { + $form = $this->getFormBuilder(); + + $adminLogin = new AdminLogin(); + + return $adminLogin->buildForm($form, array())->getForm(); + } } \ No newline at end of file diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index 68510a84c..171763f56 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -7,4 +7,7 @@ Thelia\Admin\Controller\AdminController::indexAction + + Thelia\Admin\Controller\AdminController::loginAction + \ No newline at end of file diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php index ea44af4a4..3c90ec4d8 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php @@ -145,6 +145,33 @@ class Form implements SmartyPluginInterface } } + public function formError($params, $content, \Smarty_Internal_Template $template, &$repeat) + { + + $form = $params["form"]; + if (! $form instanceof \Symfony\Component\Form\FormView) { + throw new \InvalidArgumentException("form parameter in form_error block must be an instance of + Symfony\Component\Form\FormView"); + } + + if (empty($form->vars["errors"])) { + return ""; + } + + if ($repeat) { + + $error = $form->vars["errors"]; + + $template->assign("message", $error[0]->getMessage()); + $template->assign("parameters", $error[0]->getMessageParameters()); + $template->assign("pluralization", $error[0]->getMessagePluralization()); + + + } else { + return $content; + } + } + public function getInstance($name) { if (!isset($this->formDefinition[$name])) { @@ -164,7 +191,8 @@ class Form implements SmartyPluginInterface new SmartyPluginDescriptor("block", "form", $this, "generateForm"), new SmartyPluginDescriptor("block", "form_field", $this, "formRender"), new SmartyPluginDescriptor("function", "form_field_hidden", $this, "formRenderHidden"), - new SmartyPluginDescriptor("function", "form_enctype", $this, "formEnctype") + new SmartyPluginDescriptor("function", "form_enctype", $this, "formEnctype"), + new SmartyPluginDescriptor("block", "form_error", $this, "formError") ); } } diff --git a/templates/admin/default/login.html b/templates/admin/default/login.html index 879c76cda..5c33d742e 100755 --- a/templates/admin/default/login.html +++ b/templates/admin/default/login.html @@ -18,6 +18,9 @@
{form_field_hidden form=$form} {form_field form=$form.username} + {form_error form=$form.username} + {$message} + {/form_error} {/form_field} {form_field form=$form.password}