Checkashop show helper form checkbox checked

In Prestashop module, I want to show a checkbox. For this I just took the methods of a helper class like this

$display_settings = array(
            'form' => array(
                'legend' => array(
                    'title' => $this->l( 'Display Settings' ),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                array(
                    'type' => 'checkbox',
                    'name' => 'display',
                    'values' => array(
                        'query' => array(
                            array(
                                'id' => 'show_header',
                                'name' => $this->l('show header'),
                                'val' => '1',
                                'checked' => 'checked'
                            ),
                        ),
                        'id' => 'id',
                        'name' => 'name'
                    )
                ),
                ),
                'submit' => array(
                    'title' => $this->l( 'Save Display Settings' ),
                    'class' => 'button pull-right',
                    'name' => 'save-main-display-settings',
                )
            ),
        );

      

but this only shows the checkbow (unchecked). I tried adding val to 0.1. But it didn't work for me. So can someone tell me how to make the checkbox checked in the helper class. Any help or suggestions would be really available. Thanks to

+3


source to share


2 answers


Please remove 'checked' => 'checked', this is optional. The rest of the code is fine - but this is just the definition of a FORM structure, if you want to fill it with data (the checkbox is marked as a data definition, not a structure) you need to provide the data to the HelperForm .

To enable a checkbox, set its value:



$helper = new HelperForm();
$helper->fields_value['display_show_header'] = true;

      

The name "display_show_header" is a concatenation of your names "display" and "show_header", you can also see this name in firebug when you look at the rendered checkbox.

+3


source


Complete example:



/**
 * Create the form that will be displayed in the configuration of your module.
 */
protected function renderForm()
{
    $helper = new HelperForm();
    // ...
    $helper->tpl_vars = array(
        'fields_value' => $this->getConfigFieldsValues(),
        'languages' => $this->context->controller->getLanguages(),
        'id_language' => $this->context->language->id,
    );

    return $helper->generateForm(array($this->getConfigForm()));
}
/**
 * Helper Function to fill Checkbox Fields with Data
 */
public function getConfigFieldsValues()
{
    $fields_value = array();

    $shop_groups = $this->getShopGroups();
    foreach($shop_groups as $shop_group) {
        $shop_group_id = $shop_group['id_shop_group'];

        $subshops = $this->getSubshops($shop_group['id_shop_group']);
        foreach($subshops as $subshop) {
            $shop_id = $subshop['id_shop'];
            $fields_value['mwdsubshoporderstate_' . $shop_group_id . '_' . $shop_id] = $this->getStatus($shop_id);
        }
    }

    return $fields_value;
}
/**
 * Create the structure of form.
 */
protected function getConfigForm()
{
    $form = array();
    $form_input = array();

    $shop_groups = $this->getShopGroups();
    foreach($shop_groups as $shop_group) {
        $subshops = $this->getSubshops($shop_group['id_shop_group']);

        $form_input[] = array(
            'type'      => 'checkbox',
            'label'     => $this->l($shop_group['name'] . ' :'),
            'desc'   => $this->l(''),
            'name'      => 'mwdsubshoporderstate_' . $shop_group['id_shop_group'],
            'values'    => array(
                'query' => $subshops,
                'id'    => 'id_shop',
                'name'  => 'name',
            )
        );
    }

    $form = array(
        'form' => array(
            'legend' => array(
                'title' => $this->l('Diverse Einstellungen'),
                'icon' => 'icon-cogs',
            ),
            'input' => $form_input,
            'submit' => array(
                'title' => $this->l('Speichern'),
            ),
        ),
    );

    return $form;
}

      

+1


source







All Articles