Search code examples
checkboxprestashopprestashop-1.6helper

prestashop multiple checkboxes do not save values


I can't figure out why the checkbox values are not saved in the database using helpers.

Trying to save some customers ids from my module's setting :

The array :

$custs = Customer::getCustomers();
foreach ($custs as $key => $value) {
  $options[] = array(
        'id_customer' => (int)$value['id_customer'],
        'infos' => $value['firstname'].' '.$value['lastname'].' | '.$value['email']
    );
}

The checkboxes :

'input' => array(
        array(
            'type' => 'checkbox',
            'label' => $this->l('Customers'),
            'desc' => $this->l('Select the Customers.'),
            'name' => 'MY_MODULE_CUSTOMERS',
            'values' => array(
                'query' => $options,
                'id' => 'id_customer',
                'name' => 'infos',
            ),
        ),
)

The $_POST is always empty but works well with another input. Any help will be appreciated.

Thank you.


Solution

  • I don't think its in PS docs. But with a bit of code inspecting you can see in

    Backoffice/themes/default/template/helpers/form/form.tpl

                <input type="checkbox" name="{$id_checkbox}" id="{$id_checkbox}" class="{if isset($input.class)}{$input.class}{/if}"{if isset($value.val)} value="{$value.val|escape:'html':'UTF-8'}"{/if}{if isset($fields_value[$id_checkbox]) && $fields_value[$id_checkbox]} checked="checked"{/if} />
                                                    {$value[$input.values.name]}
    

    add the porperty 'val' to option.

            $options[] = array(
            'id_carrier' => $carrier['id_carrier'],
            'name' => $carrier['name'],
            'val' => $carrier['id_carrier'],
        );
    

    Adn you get the desired serialization for the input values.

      "transportistas" => array:2 [▼
        0 => "73"
        1 => "78"
      ]