Jump to content

multiple select from the module and get them in the input


Recommended Posts

I am doing a small module in Prestashop. In that module I have used multiselect with helperform. So my code is like this

array(
'type' => 'select',
'cols' => 8,
'class' => 'chosen-product-selct selected_products ',
'multiple' => true,
'label' => $this->l('Selected Products'),
'name' => 'selected_products[]',
'options' => array(
'query' => $product_query,
'id' => 'id',
'name' => 'product_name'
),
'desc' => $this->l('Select products from products list.'),
),

Here I am saving those multiselected values to the database. But when I am doing edit no saved values has been selected in the box. The box is totally empty.

for getting the result I am doing this

public function getConfigFieldsValues() {
'selected_products[]' => Tools::getValue('selected_products', Configuration::get('selected_products')),
}

Its not showing the values that has been entered.So can someone tell me how to solve this issue? Any help and suggestions will be really appreciable. Thanks

Link to comment
Share on other sites

  • 2 months later...

Hi, here is some info that may help.

 

Assuming the multiselect input defined as above, $product_query shall be set before the definition with the array of the products that will be proposed to selection (incl. at least fields 'id' and 'product_name'). This is how the box will be populated.

 

Then, before generating the form, the helper variables shall be set as e.g.

$this->helper->tpl_vars['fields_value']['selected_products[]'] = explode(';', Configuration::get('SELECTED_PRODUCTS'));

This is how the products will be pre-selected (according to the current config) when opening the form.

As values in Configuration are made of text, the stored string is transformed to the array of pres-selected ids.

 

And finally, somewhere in postProcess, the new selection can be stored as configuration as e.g.

Configuration::updateValue('SELECTED_PRODUCTS', implode(';', Tools::getValue('selected_products')));

Just the opposite, the array of selected ids is transformed into a string that can be stored in Configuration.

 

Note that the final '[]' is used for the name of input and the helper setter, but not for the Tools getter.

A little bit confusing, isn't it?

 

Regards

Edited by erouvier29 (see edit history)
  • Like 1
Link to comment
Share on other sites

Hi, here is some info that may help.

 

Assuming the multiselect input defined as above, $product_query shall be set before the definition with the array of the products that will be proposed to selection (incl. at least fields 'id' and 'product_name'). This is how the box will be populated.

 

Then, before generating the form, the helper variables shall be set as e.g.

$this->helper->tpl_vars['fields_value']['selected_products[]'] = explode(';', Configuration::get('SELECTED_PRODUCTS'));

This is how the products will be pre-selected (according to the current config) when opening the form.

As values in Configuration are made of text, the stored string is transformed to the array of pres-selected ids.

 

And finally, somewhere in postProcess, the new selection can be stored as configuration as e.g.

Configuration::updateValue('SELECTED_PRODUCTS', implode(';', Tools::getValue('selected_products')));

Just the opposite, the array of selected ids is transformed into a string that can be stored in Configuration.

 

Note that the final '[]' is used for the name of input and the helper setter, but not for the Tools getter.

A little bit confusing, isn't it?

 

Regards

 

Hi,

This is not working. Whenever I am closing the form and again reopening the values which has been stored in the input fields are getting lost. Can you share some code for the solution?

Link to comment
Share on other sites

  • 5 months later...
  • 1 year later...
×
×
  • Create New...