Jump to content

How to set multiple select options for a select input type with an HelperForm


gr4devel

Recommended Posts

How I'm supposed to set the selected options for a multiple select input type inside an HelperForm?

 

Here's the code that should handle the display of the over mentioned select input type:

...
 array(
  'type' => 'select',
  'name' => 'field_name[]',
  'id' => 'field_name',
  'label' => $this->l('Multiple value select field'),
  'desc' => $this->l('...'),
  'multiple' => true,
  'options' => array(
    'query' => $field_values,
    'id' => 'id_field_value',
    'name' => 'field_value_name'
  )
),
...

Just for the record, the trailing square brackets after the input type name are needed to properly populate the POST variable with all the multiple value selected.

 

Here's the code that should handle the correct selection of the already saved (i.e. selected) options:
 

...
$helper->tpl_vars = array(
  'uri' => $this->getPathUri(),
  'fields_value' => [
    ...
    'field_values[]'   => $field_values,
    ...
  ],
  'languages' => $this->context->controller->getLanguages(),
  'id_language' => $current_back_office_language
);
...

As always there is no clear documentation that clarifies how to structure the $field_values field.

 

I've tried a bunch of different options like a plain array of indexes or an hash with each index identified by the actual name (i.e. string) but nothing seems to work.

 

Every help will be appreciated.

 

Thanks.

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

  • 1 year later...

I have answered this question on stackoverflow and on this forum (p=2179775).

Here I am providing the same answer which I think should solve the issue you have faced:

 

I found a solution to the problem with Prestashop multiple select form helper issue.
 
In order for the select box to work properly there are the following requirements:
 
 1. Input name should have '[]'. For example: manufacturer_ids[]
 2. $fields_value array should have this element with the same name: manufacturer_ids[] not manufacturer_ids.
 3. The value of the $fields_value array's corresponding item should have selected values as array. For example: 
 
    $fields_value['manufacturer_ids[]'] = array("120", "145");
 
 
I save the submitted values as sarialized string in Database, so I am handling it this way:
 
        $selectedManufacturers = @unserialize($manufacturerIdsTakenFromDb);
        if ($selectedManufacturers === false && $selectedManufacturers !== 'b:0;') {
            $selectedManufacturers = array();
        }        
        $fields_value['manufacturer_ids[]'] = $selectedManufacturers;
 
And my code in form definition looks like this:
 
    array(
        'type' => 'select',
        'label' => $this->l('Manufacturers'),
        'name' => 'manufacturer_ids[]',
        'multiple' => true,
        'options' => array(
            'query' => array(
                array('key' => '1', 'name' => 'Apple'),
                array('key' => '2', 'name' => 'Samsung'),
                array('key' => '3', 'name' => 'HTC'),
            ),
            'id' => 'key',
            'name' => 'name'
        )
    ),
 
If your code does satisfy all these and it still does not work, please let me know, I will be happy to assist you to fix it.

 

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

i found the solution, due in this "forum comunity" there's no "comunity" only "forum sales"

 

THE COMPLETE CODE AND ALL THE STEPS ARE AT: https://groups.google.com/forum/m/?hl=es#!topic/venenuxsarisari/z8vfPsvFFjk

here i put only the most important parts..

 

as mentioned int he previous link, added a new fiel in the model definition, class and the table sql

 

this method permits to stored in the db as "1,2,3" so you can use only a single field to relation that multiple selected values, 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 realtional table event stored in single field

 

then in the helper form list array of inputs define a select as:

 


                array(
                     'type' => 'select',
                    'label' => $this->l('Select and employee'),
                    'name' => 'id_employee_tech',
                    'required' => false,
                    'col' => '6',
                    'default_value' => (int)Tools::getValue('id_employee_tech'),
                    'options' => array(
                        'query' => Employee::getEmployees(true), // el true es que solo los que estan activos
                        'id' => 'id_employee',
                        'name' => 'firstname',
                        'default' => array(
                            'value' => '',
                            'label' => $this->l('ninguno')
                        )
                    )
                ),

an then override the post process too

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


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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...