prestashop show helper form checkbox checked

NewUser picture NewUser · Aug 19, 2014 · Viewed 7.5k times · Source

In Prestashop module I want to show a checkbox checked. For that I just took the helper class methods 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 one is showing only checkbow (not checked). I tried to chnage val into 0,1. But it did not worked for me. So can someone tell me how to make a checkbox checked in helper class. Any help or suggestions will be really appreceable. Thanks

Answer

Adiasz picture Adiasz · Aug 26, 2014

Please remove 'checked' => 'checked' it is not necessary. Rest of your code is okay - but it is only FORM structure definition, if you want to fill it with data (checked checkbox is data definition not structure) you need to provide data to HelperForm.

To make checkbox checked set it value by:

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

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