create smarty function for displaying hidden form fields
This commit is contained in:
@@ -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 = '<input type="hidden" name="%s" value="%s">';
|
||||
|
||||
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")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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__))
|
||||
)
|
||||
)
|
||||
|
||||
@@ -33,7 +33,8 @@ class CustomerCreation extends AbstractType
|
||||
{
|
||||
return $builder->add("name", "text")
|
||||
->add("email", "email")
|
||||
->add('age', 'integer');
|
||||
->add('age', 'integer')
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,17 +9,17 @@ An image from asset directory :
|
||||
</div>
|
||||
<form method="post" action="index_dev.php" >
|
||||
{form name="thelia.customer.creation"}
|
||||
{form_render form=$form._token}
|
||||
<input type="hidden" name="{$name}" value="{$value}">
|
||||
{/form_render}
|
||||
|
||||
{form_render form=$form.email}
|
||||
{form_field_hidden form=$form}
|
||||
{form_field form=$form.email}
|
||||
{intl l='email'} : <input type="text" name="{$name}">
|
||||
{/form_render}
|
||||
{/form_field}
|
||||
|
||||
{form_render form=$form.name}
|
||||
{form_field form=$form.name}
|
||||
{intl l='name'} : <input type="text" name="{$name}">
|
||||
{/form_render}
|
||||
{/form_field}
|
||||
{form_field form=$form.age}
|
||||
{intl l='age'} : <input type="text" name="{$name}">
|
||||
{/form_field}
|
||||
{/form}
|
||||
</form>
|
||||
<div>
|
||||
|
||||
Reference in New Issue
Block a user