render form field basic info (name and value)

This commit is contained in:
Manuel Raynaud
2013-06-24 16:13:43 +02:00
parent 84bc1a8268
commit e29dafdd08
2 changed files with 46 additions and 11 deletions

View File

@@ -27,6 +27,7 @@ use Thelia\Core\Template\Element\Exception\ElementNotFoundException;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Thelia\Core\Template\Smarty\SmartyPluginDescriptor; use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
use Thelia\Core\Template\Smarty\SmartyPluginInterface; use Thelia\Core\Template\Smarty\SmartyPluginInterface;
use Thelia\Log\Tlog;
class Form implements SmartyPluginInterface class Form implements SmartyPluginInterface
{ {
@@ -53,21 +54,42 @@ class Form implements SmartyPluginInterface
} }
} }
public function generateForm($params, $content, $template, &$repeat) public function generateForm($params, $content, \Smarty_Internal_Template $template, &$repeat)
{ {
if (empty($params['name'])) { if ($repeat) {
throw new \InvalidArgumentException("Missing 'name' parameter in form arguments"); if (empty($params['name'])) {
throw new \InvalidArgumentException("Missing 'name' parameter in form arguments");
}
$form = new BaseForm($this->request);
$formBuilder = $form->getFormBuilder()->createBuilder();
$instance = $this->getInstance($params['name']);
$instance = $instance->buildForm($formBuilder, array());
$template->assign("form", $instance->getForm()->createView());
} else {
return $content;
} }
}
$form = new BaseForm($this->request); public function formRender($params, $content, \Smarty_Internal_Template $template, &$repeat)
$formBuilder = $form->getFormBuilder()->createBuilder(); {
if ($repeat) {
$instance = $this->getInstance($params['name']); $form = $params["form"];
$instance = $instance->buildForm($formBuilder, array());
var_dump($instance->getForm()->createView()); exit; if (! $form instanceof \Symfony\Component\Form\FormView) {
$template->assign("form", $instance->getForm()->createView()); throw new \InvalidArgumentException("form parameter in form_render block must be an instance of
Symfony\Component\Form\FormView");
}
$template->assign("name", $form->vars["name"]);
$template->assign("value", $form->vars["data"]);
} else {
return $content;
}
} }
public function getInstance($name) public function getInstance($name)
@@ -86,7 +108,8 @@ class Form implements SmartyPluginInterface
public function getPluginDescriptors() public function getPluginDescriptors()
{ {
return array( return array(
new SmartyPluginDescriptor("block", "form", $this, "generateForm") new SmartyPluginDescriptor("block", "form", $this, "generateForm"),
new SmartyPluginDescriptor("block", "form_render", $this, "formRender")
); );
} }
} }

View File

@@ -7,9 +7,21 @@ An image from asset directory :
<div> <div>
{intl l='An internationalized string'} {intl l='An internationalized string'}
</div> </div>
<form method="post" action="index_dev.php" >
{form name="thelia.customer.creation"} {form name="thelia.customer.creation"}
{$form.name} {form_render form=$form._token}
<input type="hidden" name="{$name}" value="{$value}">
{/form_render}
{form_render form=$form.email}
{intl l='email'} : <input type="text" name="{$name}">
{/form_render}
{form_render form=$form.name}
{intl l='name'} : <input type="text" name="{$name}">
{/form_render}
{/form} {/form}
</form>
<div> <div>
jQuery data: <span id="jquery_block"></span> jQuery data: <span id="jquery_block"></span>
</div> </div>