Prestashop set multiple select from the module and get them in the input

大兔子大兔子 提交于 2019-12-25 04:53:15

问题


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


回答1:


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.




回答2:


the answer previous was a single copy paste from prestashop forums that does not solve it, due does not explain how...

here i explain correctly how, must capture and stored in special way the values, in this example i demonstrate to store as "1,2,3,6,8" using a single field

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 relational table event stored in single field

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

at the begining dont foget 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['id_employee[]'] = 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' => '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"



来源:https://stackoverflow.com/questions/27731117/prestashop-set-multiple-select-from-the-module-and-get-them-in-the-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!