add form_error block

This commit is contained in:
Manuel Raynaud
2013-06-25 16:54:42 +02:00
parent b2ee3c3ef8
commit 8dd43d6779
4 changed files with 46 additions and 10 deletions

View File

@@ -30,11 +30,7 @@ class AdminController extends BaseAdminController {
public function indexAction() public function indexAction()
{ {
$form = $this->getFormBuilder(); $form = $this->getLoginForm();
$adminLogin = new AdminLogin();
$form = $adminLogin->buildForm($form, array())->getForm();
$request = $this->getRequest(); $request = $this->getRequest();
@@ -42,10 +38,7 @@ class AdminController extends BaseAdminController {
$form->bind($request); $form->bind($request);
if($form->isValid()) { if($form->isValid()) {
//TODO echo "valid"; exit;
} else {
//TODO
} }
} }
@@ -53,4 +46,13 @@ class AdminController extends BaseAdminController {
"form" => $form->createView() "form" => $form->createView()
)); ));
} }
protected function getLoginForm()
{
$form = $this->getFormBuilder();
$adminLogin = new AdminLogin();
return $adminLogin->buildForm($form, array())->getForm();
}
} }

View File

@@ -7,4 +7,7 @@
<route id="admin" path="/admin"> <route id="admin" path="/admin">
<default key="_controller">Thelia\Admin\Controller\AdminController::indexAction</default> <default key="_controller">Thelia\Admin\Controller\AdminController::indexAction</default>
</route> </route>
<route id="admin.login" path="/admin/login">
<default key="_controller">Thelia\Admin\Controller\AdminController::loginAction</default>
</route>
</routes> </routes>

View File

@@ -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) public function getInstance($name)
{ {
if (!isset($this->formDefinition[$name])) { if (!isset($this->formDefinition[$name])) {
@@ -164,7 +191,8 @@ class Form implements SmartyPluginInterface
new SmartyPluginDescriptor("block", "form", $this, "generateForm"), new SmartyPluginDescriptor("block", "form", $this, "generateForm"),
new SmartyPluginDescriptor("block", "form_field", $this, "formRender"), new SmartyPluginDescriptor("block", "form_field", $this, "formRender"),
new SmartyPluginDescriptor("function", "form_field_hidden", $this, "formRenderHidden"), 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")
); );
} }
} }

View File

@@ -18,6 +18,9 @@
<form action="/admin" method="post" class="well form-inline" {form_enctype form=$form}> <form action="/admin" method="post" class="well form-inline" {form_enctype form=$form}>
{form_field_hidden form=$form} {form_field_hidden form=$form}
{form_field form=$form.username} {form_field form=$form.username}
{form_error form=$form.username}
{$message}
{/form_error}
<input type="text" class="input" placeholder="{intl l='E-mail address'}" name="{$name}" /> <input type="text" class="input" placeholder="{intl l='E-mail address'}" name="{$name}" />
{/form_field} {/form_field}
{form_field form=$form.password} {form_field form=$form.password}