From fe92cc546aec71df79f59183b5feb28bb4878a29 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Tue, 25 Jun 2013 10:35:00 +0200 Subject: [PATCH] create smarty function for displaying hidden form fields --- .../Core/Template/Smarty/Plugins/Form.php | 43 ++++++++++++++++--- .../Template/Smarty/Plugins/TheliaLoop.php | 4 +- core/lib/Thelia/Form/BaseForm.php | 16 +++---- core/lib/Thelia/Form/CustomerCreation.php | 3 +- templates/smarty-sample/index.html | 16 +++---- 5 files changed, 55 insertions(+), 27 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php index ff5d298cf..0dbfb5f01 100644 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/Form.php @@ -22,6 +22,7 @@ /*************************************************************************************/ namespace Thelia\Core\Template\Smarty\Plugins; +use Symfony\Component\Form\FormView; use Thelia\Form\BaseForm; use Thelia\Core\Template\Element\Exception\ElementNotFoundException; use Symfony\Component\HttpFoundation\Request; @@ -57,16 +58,16 @@ class Form implements SmartyPluginInterface public function generateForm($params, $content, \Smarty_Internal_Template $template, &$repeat) { if ($repeat) { + if (empty($params['name'])) { throw new \InvalidArgumentException("Missing 'name' parameter in form arguments"); } - $form = new BaseForm($this->request); - $formBuilder = $form->getFormBuilder()->createBuilder(); + $form = BaseForm::getFormFactory($this->request); + $formBuilder = $form->createBuilder('form'); $instance = $this->getInstance($params['name']); $instance = $instance->buildForm($formBuilder, array()); - $template->assign("form", $instance->getForm()->createView()); } else { return $content; @@ -80,18 +81,47 @@ class Form implements SmartyPluginInterface $form = $params["form"]; if (! $form instanceof \Symfony\Component\Form\FormView) { - throw new \InvalidArgumentException("form parameter in form_render block must be an instance of + throw new \InvalidArgumentException("form parameter in form_field block must be an instance of Symfony\Component\Form\FormView"); } $template->assign("name", $form->vars["name"]); - $template->assign("value", $form->vars["data"]); + $template->assign("value", $form->vars["value"]); + + $form->setRendered(); } else { return $content; } } + public function formRenderHidden($params, $template) + { + $form = $params["form"]; + + $field = ''; + + if (! $form instanceof \Symfony\Component\Form\FormView) { + throw new \InvalidArgumentException("form parameter in form_field_hidden function must be an instance of + Symfony\Component\Form\FormView"); + } + + $return = ""; + + foreach ($form->getIterator() as $row) { + if ($this->isHidden($row)) { + $return .= sprintf($field, $row->vars["name"], $row->vars["value"]); + } + } + + return $return; + } + + protected function isHidden(FormView $formView) + { + return array_search("hidden", $formView->vars["block_prefixes"]); + } + public function getInstance($name) { if (!isset($this->formDefinition[$name])) { @@ -109,7 +139,8 @@ class Form implements SmartyPluginInterface { return array( new SmartyPluginDescriptor("block", "form", $this, "generateForm"), - new SmartyPluginDescriptor("block", "form_render", $this, "formRender") + new SmartyPluginDescriptor("block", "form_field", $this, "formRender"), + new SmartyPluginDescriptor("function", "form_field_hidden", $this, "formRenderHidden") ); } } diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php index 3258f36c5..24a7ad609 100755 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php @@ -86,9 +86,9 @@ class TheliaLoop implements SmartyPluginInterface } if ($loopResults->valid()) { + $loopResultRow = $loopResults->current(); - - foreach($loopResultRow->getVarVal() as $var => $val) { + foreach($loopResultRow->getVarVal() as $var => $val) { $template->assign(substr($var, 1), $val); } diff --git a/core/lib/Thelia/Form/BaseForm.php b/core/lib/Thelia/Form/BaseForm.php index e16106c1b..bd5dbdaef 100644 --- a/core/lib/Thelia/Form/BaseForm.php +++ b/core/lib/Thelia/Form/BaseForm.php @@ -32,22 +32,18 @@ use Thelia\Model\ConfigQuery; class BaseForm { - protected $request; - - public function __construct(Request $request) - { - $this->request = $request; - } - - - public function getFormBuilder() + /** + * @param Request $request + * @return \Symfony\Component\Form\FormFactoryInterface + */ + public static function getFormFactory(Request $request) { $form = Forms::createFormFactoryBuilder() ->addExtension(new HttpFoundationExtension()) ->addExtension( new CsrfExtension( new SessionCsrfProvider( - $this->request->getSession(), + $request->getSession(), ConfigQuery::read("form.secret", md5(__DIR__)) ) ) diff --git a/core/lib/Thelia/Form/CustomerCreation.php b/core/lib/Thelia/Form/CustomerCreation.php index 4c70a0177..f6215387e 100644 --- a/core/lib/Thelia/Form/CustomerCreation.php +++ b/core/lib/Thelia/Form/CustomerCreation.php @@ -33,7 +33,8 @@ class CustomerCreation extends AbstractType { return $builder->add("name", "text") ->add("email", "email") - ->add('age', 'integer'); + ->add('age', 'integer') + ; } /** diff --git a/templates/smarty-sample/index.html b/templates/smarty-sample/index.html index be5e00a24..ac4ae2a0d 100755 --- a/templates/smarty-sample/index.html +++ b/templates/smarty-sample/index.html @@ -9,17 +9,17 @@ An image from asset directory :
{form name="thelia.customer.creation"} - {form_render form=$form._token} - - {/form_render} - - {form_render form=$form.email} + {form_field_hidden form=$form} + {form_field form=$form.email} {intl l='email'} : - {/form_render} + {/form_field} - {form_render form=$form.name} + {form_field form=$form.name} {intl l='name'} : - {/form_render} + {/form_field} + {form_field form=$form.age} + {intl l='age'} : + {/form_field} {/form}