0

我尝试在 PrestaShop 1.5 的模块配置中添加复选框。我在手册中使用 HelperForm 但 $helper->fields_value 为空

 public function getContent()
{
    $output = null;

    if (Tools::isSubmit('submit'.$this->name))
    {
        $fast_email = strval(Tools::getValue('fast_email'));
        if (!$fast_email  || empty($fast_email) || !Validate::isGenericName($fast_email))
            $output .= $this->displayError( $this->l('Invalid Configuration value') );
        else
        {
            Configuration::updateValue('fast_email', $fast_email);
            $output .= $this->displayConfirmation($this->l('Settings updated'));
        }
        $fast_email_sender =  strval(Tools::getValue('fast_email_sender'));

        if ( !Validate::isGenericName($fast_email_sender))
            $output .= $this->displayError( $this->l('Invalid Configuration value') );
        else
        {
            Configuration::updateValue('fast_email_sender', $fast_email_sender);

            $output .= $this->displayConfirmation($this->l('Settings updated'));
        }
        $fast_phone = strval(Tools::getValue('fast_phone'));

        Configuration::updateValue('fast_phone', $fast_phone);
        var_dump(Tools::getValue('fast_phone'));
        $output .= $this->displayConfirmation($this->l('Settings updated'));
    }
    return $output.$this->displayForm();
}
    public function displayForm()
{
    // Get default Language
    $default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
    $options = array(
        array(
            'id_option' => 1,                 // The value of the 'value' attribute of the <option> tag.
            'name' => 'email'              // The value of the text content of the  <option> tag.
        ),
        array(
            'id_option' => 2,
            'name' => 'phone'
        ),
    );
 //   var_dump($options);
    // Init Fields form array
    $fields_form[0]['form'] = array(
        'legend' => array(
            'title' => $this->l('Settings'),
        ),
        'input' => array(
            array(
                'type' => 'text',
                'label' => $this->l('admin email'),
                'name' => 'fast_email',
                'size' => 20,
                'required' => true
            )
        ,
            array(
                'type'    => 'checkbox',                         // This is an <input type="checkbox"> tag.
                'label'   => $this->l('Options'),                // The <label> for this <input> tag.
                'desc'    => $this->l('Choose options.'),        // A help text, displayed right next to the <input> tag.
                'name'    => 'fast_phone',                          // The content of the 'id' attribute of the <input> tag.
                'values'  => array(
                    'query' => $options,                           // $options contains the data itself.
                    'id'    => 'id_option',                        // The value of the 'id' key must be the same as the key for 'value' attribute of the <option> tag in each $options sub-array.
                    'name'  => 'name'                              // The value of the 'name' key must be the same as the key for the text content of the <option> tag in each $options sub-array.
                ),
            ),
            array(
                'type'      => 'radio',
                'label'     => $this->l('email'),
                'name'      => 'fast_email_sender',                              // The content of the 'id' attribute of the <input> tag.
                'required'  => true,
                'class'     => 't',
                'values'    => array(                                 // $values contains the data itself.
                    array(
                        'id'    => 'active_on',                           // The content of the 'id' attribute of the <input> tag, and of the 'for' attribute for the <label> tag.
                        'value' => 1,                                     // The content of the 'value' attribute of the <input> tag.
                        'label' => $this->l('Enabled')                    // The <label> for this radio button.
                    ),
                    array(
                        'id'    => 'active_off',
                        'value' => 0,
                        'label' => $this->l('Disabled')
                    )
                ),
            )
        ),

        'submit' => array(
            'title' => $this->l('Save'),
            'class' => 'button'
        )
    );

    $helper = new HelperForm();

    // Module, t    oken and currentIndex
    $helper->module = $this;
    $helper->name_controller = $this->name;
    $helper->token = Tools::getAdminTokenLite('AdminModules');
    $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name;

    // Language
    $helper->default_form_language = $default_lang;
    $helper->allow_employee_form_lang = $default_lang;

    // Title and toolbar
    $helper->title = $this->displayName;
    $helper->show_toolbar = true;        // false -> remove toolbar
    $helper->toolbar_scroll = true;      // yes - > Toolbar is always visible on the top of the screen.
    $helper->submit_action = 'submit'.$this->name;
    $helper->toolbar_btn = array(
        'save' =>
        array(
            'desc' => $this->l('Save'),
            'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name.
                '&token='.Tools::getAdminTokenLite('AdminModules'),
        ),
        'back' => array(
            'href' => AdminController::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModules'),
            'desc' => $this->l('Back to list')
        )
    );
    // Load current value
    $helper->fields_value['fast_email'] = Configuration::get('fast_email');
    $helper->fields_value['fast_email_sender'] = Configuration::get('fast_email_sender');
    $helper->fields_value['fast_phone'] = Configuration::get('fast_phone');
    return $helper->generateForm($fields_form);
}

我在检查时添加了安装Configuration::updateValue('fast_phone','')并保存它不会更新。我检查配置并在提交前后为空

4

1 回答 1

1

我知道这个剧院很旧,但对于其他人,我给出了解决方案:根据文档,getContent 方法必须这样编写:

public function getContent()
{
    $output = null;
    // Save settings values
    if (Tools::isSubmit('submit'.$this->name)) // Check if form is submitted
    {
        Configuration::updateValue('MODULE_OPTION1', Tools::getValue('MODULE_OPTION1'));
        Configuration::updateValue('MODULE_OPTION2', Tools::getValue('MODULE_OPTION2'));
        Configuration::updateValue('MODULE_OPTION3', Tools::getValue('MODULE_OPTION3'));
        $output .= $this->displayConfirmation($this->l('Settings updated'));
    }
    return $output.$this->displayForm();
}

当然可以改进:检查值...

于 2015-11-18T09:22:43.183 回答