如何在 PrestaShop 1.6 中使用 FrontController 中的 HelperForm

How to use HelperForm from the FrontController in PrestaShop 1.6

我试图用 helperForm 做一个表单,但这是不可能的。 我一直在关注官方文档,但 FrontControler 对我来说是不可能的。

http://doc.prestashop.com/display/PS16/Using+the+HelperForm+class

我可以这样做吗?

向我显示的错误是:

Uncaught --> Smarty: Unable to load template file 'helpers/form/form.tpl'

而我的 class 是:

public function renderForm()
  { 
    $fields_form = array( 'form' => array(
      'legend' => array(       
        'title' => $this->module->l('Edit carrier'),       
        'image' => '../img/admin/icon_to_display.gif'   
      ),   
      'input' => array(       
        array(           
          'type' => 'text',
          'name' => 'shipping_method',
         ),
      ),
      'submit' => array(
        'title' => $this->module->l('Save'),       
        'class' => 'btn btn-default pull-right'   
      )
    ));

    $helper = new HelperForm();
    $helper->show_toolbar = false;
    $helper->table = $this->table;
    $this->fields_form = array();

    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submitBlockCart';

    return $helper->generateForm(array($fields_form));
}

我要疯了....

我已经在很多线程上看到过这种错误。您的 admin/themes/your_theme/template/helpers/form/form.tpl 是否在您的服务器上并且具有正确的访问权限?

有时由于奇怪的原因整个 helpers 目录丢失。


编辑 助手表格仅适用于后台。您不能在 FrontController 上下文中使用它。

答案很简单:您不能从前端使用 HelperForm。

据我所知你只能在后台使用helperForm,所以我认为你不能在前台使用。

实际上,我不知道你是否应该,但你肯定可以在自定义中使用HelperForm模块。

看看 Helper class 中的函数 createTemplateHelperForm 扩展)在 /classes/helper/Helper.php 中,特别是在这块代码:

elseif ($this->module) {
    $override_tpl_path = _PS_MODULE_DIR_.$this->module->name.'/views/templates/admin/_configure/'.$this->base_folder.$tpl_name;
}

您可以看到,如果您定义 属性 module,助手将使用模块文件夹内的模板 form.tpl,在 /views/templates/admin/_configure/helpers/form/.[=25 下=]

所以你需要做的是:

  1. form.tpladmin/themes/default/template/helpers/form/form.tpl复制到模块中的上述路径
  2. 调用generateForm()前放置$helper->module = $this;

注意: 此外,您可能还想定义 $helper->tpl_vars 以便能够使用输入的 属性 name