prestashop multiple checkboxes do not save values

2019-09-09 21:14发布

问题:

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.

回答1:

Your code is correct, I tried it and this is result http://screencast.com/t/wfsW86iJj

  1. You have to click at least one checkbox.
  2. Show data on server :

        print_r($_POST);
        die();
    


回答2:

a better could be using groupbox but its quite difficult, take a look to the AdminCustomers controller class in the controllers directory of the prestachop, this has a multiselect group that used a relational table event stored in single field

If you want to be easy, using a single field to store in the database, take a look to THE COMPLETE CODE AND ALL THE STEPS AT: https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk

at the begining dont forget to added that line:

// aqui el truco de guardar el multiselect como una secuencia separada por comas, mejor es serializada pero bueh
$this->fields_value['MY_MODULE_CUSTOMERS[]'] = explode(',',$obj->id_employee);

this $obj are the representation of the loaded previous stored value when go to edit ... from that object, get the stored value of the field of your multiselect, stored as "1,3,4,6"

and the in the field form helper list of inputs define the select multiple as:

            array(
                 'type' => 'checkbox',
                'label' => $this->l('Select and employee'),
                'name' => 'MY_MODULE_CUSTOMERS[]',
                'required' => false,
                'col' => '6',
                'default_value' => (int)Tools::getValue('id_employee_tech'),
                'values' => array(
            'query' => $options,
            'id' => 'id_customer',
            'name' => 'infos',
        ),
            ),

an then override the post process too

public function postProcess()
{
    if (Tools::isSubmit('submitTallerOrden'))
    {
        $_POST['MY_MODULE_CUSTOMERS'] = implode(',', Tools::getValue('MY_MODULE_CUSTOMERS'));
    }
    parent::postProcess();
}

this make stored in the db as "1,2,3"