Prestashop HelperForm:切换按钮

时间:2016-01-26 10:03:17

标签: prestashop

我正在使用Prestashop HelperForm来生成切换按钮。按钮的状态取决于从数据库生成的数据。问题是该按钮始终设置为false。

以下是代码:

$fields_form = array(
        'form' => array(
            'legend' => array(
                'title' => $this->l('Champs pour feuille de soin'),
                'icon' => 'icon-pencil'
            ),

            'input' => array(
                array(
                    'type' => 'hidden',
                    'name' => 'id_product',
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->l('Label'),
                    'name' => 'contenu1'
                ),
                array(

                    'type' => 'hidden',
                    'name' => 'id_customization_field1',
                ),

                array(
                    'type' => 'radio',
                    'label' => $this->l('required'),
                    'name' => 'relab1',
                    'is_bool' => false,
                    'desc' => $this->l('required'),
                    'values' => array(
                        array(
                            'id' => 'label1_on',
                            'value' => 1,
                            'label' => $this->l('Enabled')
                        ),
                        array(
                            'id' => 'label1_off',
                            'value' => 0,
                            'label' => $this->l('Disabled')
                        )
                    )
                ),
                array(
                    'type' => 'textarea',
                    'label' => $this->l('Label'),
                    'name' => 'contenu2'
                ),
                array(

                    'type' => 'hidden',
                    'name' => 'id_customization_field2',
                ),
                array(
                    'type' => 'radio',
                    'label' => $this->l('required'),
                    'name' => 'relab2',
                    'is_bool' => false,
                    'desc' => $this->l('required'),
                    'values' => array(
                        array(
                            'id' => 'active_on',
                            'label' => $this->l('Enabled')
                        ),
                        array(
                            'id' => 'active_off',
                            'label' => $this->l('Disabled')
                        )
                    )
                ),


                array(
                    'type' => 'textarea',
                    'label' => $this->l('Label'),
                    'name' => 'contenu3'
                ),
                array(

                    'type' => 'hidden',
                    'name' => 'id_customization_field3',
                ),

                array(
                    'type' => 'switch',
                    'label' => $this->l('required'),
                    'name' => 'relab3',
                    'is_bool' => true,
                    'desc' => $this->l('required'),
                    'values' => array(
                        array(
                            'id' => 'label3_on',
                            'value' => 1,
                            'label' => $this->l('Enabled')
                        ),
                        array(
                            'id' => 'label3_off',
                            'value' => 0,
                            'label' => $this->l('Disabled')
                        )
                    )
                ),


                array(
                    'type' => 'textarea',
                    'label' => $this->l('Label'),
                    'name' => 'contenu4'
                ),
                array(

                    'type' => 'hidden',
                    'name' => 'id_customization_field4',
                ),

                array(
                    'type' => 'checkbox',
                    'label' => $this->l('Required'),
                    'name' => 'label4',
                    'class' => 't',
                    'multiple' => true,
                    'values' => array(
                        'query' => array($label3),
                        'id' => 'label4',
                        'name' => 'label4',
                        'expand' => array(
                            'default' => 'show',
                            'show' => array('text' => $this->l('show'), 'icon' => 'plus-sign-alt'),
                            'hide' => array('text' => $this->l('hide'), 'icon' => 'minus-sign-alt')
                        ),
                    )
                ),


            ),
            'submit' => array(
                'title' => $this->l('Save'),
                'name' => $this->l('submitAddproduct'),
            )
        ),
    );

    $helper = new HelperForm();
    $helper->show_toolbar = false;
    $helper->table = $this->table;
    $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
    $helper->default_form_language = $lang->id;
    $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
    $helper->identifier = $this->identifier;
    $helper->submit_action = 'submitUpdate';
    $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');
    $helper->tpl_vars = array(
        $helper->fields_value['id_product'] = Tools::getValue('id_product'),
        'relab4' => 1,

        'languages' => $this->context->controller->getLanguages(),
        'id_language' => $this->context->language->id
    );

    $helper->fields_value['contenu1'] = $contenu1;
    $helper->fields_value['contenu2'] = $contenu2;
    $helper->fields_value['contenu3'] = $contenu3;
    $helper->fields_value['contenu4'] = $contenu4;
    $helper->fields_value['relab1'] = (int)$relab1;
    $helper->fields_value['relab2'] = (int)$relab2;
    $helper->fields_value['relab3'] = (int)$relab3;
    $helper->fields_value['relab4'] = (int)$relab4;
    $helper->fields_value['id_customization_field1'] = $id_customization_field1;
    $helper->fields_value['id_customization_field2'] = $id_customization_field2;
    $helper->fields_value['id_customization_field3'] = $id_customization_field3;
    $helper->fields_value['id_customization_field4'] = $id_customization_field4;


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

4 个答案:

答案 0 :(得分:1)

我猜你也有这部分代码($helper是你的HelperForm()对象):

$helper->tpl_vars = array(
    'fields_value' => $this->getConfigFieldsValues(),
    'languages' => $this->context->controller->getLanguages(),
    'id_language' => $this->context->language->id
);

因此,使用此功能,您需要返回relab2值:

public function getConfigFieldsValues()
{
    return array(
        'relab2' => 1 // your value, 1 or 0
        // other form values
    );
}

答案 1 :(得分:0)

您需要向include template[variable=customer] 对象提供表单值。

以下是一个例子:

HelperForm

答案 2 :(得分:0)

您好我在prestashop 1.6和1.7上遇到了同样的问题,我终于找到了问题,所以我将与您分享我的发现。

所以这是我的开关:

 array(
    'type' => 'switch',
    'label' => $this->trans('Captcha Test Mode', array(), 'Admin.Shipping.Feature'),
     'name' => 'CORE_CAPTCHA_TEST_MODE',
     'required' => false,
     'class' => 't',
     'is_bool' => true,
      'values' => array(
            array(
                'id' => 'active_on',
                'value' => 1,
                'label' => $this->trans('Enabled', array(), 'Admin.Global'),
                 ),
            array(
                'id' => 'active_off',
                'value' => 0,
                'label' => $this->trans('Disabled', array(), 'Admin.Global'),
                 ),
       ),

如您所见,它必须有一个明确的名称CORE_CAPTCHA_TEST_MODE。

事情变得有趣的是getConfigFormValues()方法:

 protected function getConfigFormValues()
{
    return array(
        'CORE_CAPTCHA_TEST_MODE' => Tools::getValue('CORE_CAPTCHA_TEST_MODE', Configuration::get('CORE_CAPTCHA_TEST_MODE', true)),
    );
}

与任何其他类型的字段不同,其中一个简单的Configuration :: get足以在为开关加载表单时在字段中设置值,我们需要添加Tools :: getValue(...

答案 3 :(得分:0)

也有同样的问题,机器人问题在其他地方,只是开关按钮本身什么都不做,您必须向其提供价值数据,然后更改才会显示。

和其他注明的开关输入本身一样:

         array(
          'type' => 'switch',
          'label' => $this->l('Display map'),
          'name' => 'my_data',
          'is_bool' => true,
          'values' => array(
              array(
                  'id' => 'label2_on',
                  'value' => 1,
                  'label' => $this->l('Enabled')
              ),
              array(
                  'id' => 'label2_off',
                  'value' => 0,
                  'label' => $this->l('Disabled')
              )
          )
      ),

并在下面提供保存的数据

    $helper->fields_value['my_data'] = Configuration::get('my_data');