Magento 1.7 – Preselect Saved Values in Custom Admin-Grid Multiselect

adminadminformdevelopmentgridmagento-1.7

I have a custom backend-grid for some shipping-methods and I have the following problem:

Everytime I want to edit one of my grid-entries, the country-multi-select is resetted to blank. I´ve been trying a lot to get the previously saved values as preselected values in this multiselect, but I was not successful.

*_Edit_Form.php _prepareForm() excerpt:


$fieldset_rules->addField('allowedcountries', 'multiselect', array(
            'label'     => Mage::helper('kortwotze_shippingmadesimple')->__('Available Countries'),
            'class'     => 'required-entry',
            'required'  => true,
            'name'      => 'allowedcountries[]',
            'values'    => $countryList,
            'value'     => $model->getAllowedCountries(), // Not sure, what exactly to return...
            'note'      => Mage::helper('kortwotze_shippingmadesimple')->__('Mark the countries, for which this shipping method applies.'),
        ));

I have the suspicion, that my models have to return a distinct format of that data I want to use, but I have no clue. It may be important, that I serialize the selection while saving and save it as serialized string into a TEXT database-column.

Any help appreciated! I would love to give out more information if needed, but honestly: I am not sure, what I have to provide 😀

Additional Info to oleksii.svarychevskyi:

$countryList looks like



2014-02-03T12:52:48+00:00 DEBUG (7): Array
(
    [0] => Array
        (
            [value] => 
            [label] => 1
        )

    [1] => Array
        (
            [value] => AF
            [label] => Afghanistan
        )

    [2] => Array
        (
            [value] => AX
            [label] => Alandinseln
        )

    [3] => Array
        (
            [value] => AL
            [label] => Albanien
        )

I tried with 'value' => array('DE') // Uppercase/Lowercase - no success. :/
I also tried with 'value' => 'DE' which also turned out not successful.

Solution

I was really stupid, thinking to save arrays from multiselects on my own. I tried to reinvent the wheel and build something shaped like a parrot.

Just use 'type' => 'multiselect' in your Grid/Edit and add something like this to your saveAction()

            foreach ($data as $key => $value) {
                if (is_array($value)) {
                    $data[$key] = $data[$key] = implode(',',$this->getRequest()->getParam($key));
                }
            }

(shamelessly stolen from https://stackoverflow.com/questions/14056140/magento-saving-multiselect-array-on-custom-model – thanks for that solution as well! )

Best Answer

I'll try to help you

values attribute should contain the array like this

array(
    array(
        'label' => 'England',
        'value' => 'en'
    ),
    array(
        'label' => 'USA',
        'value' => 'us'
    ),
    ...
)

value attribute should contain array or coma separated list of selected values from value of values attribute.

array('us', 'en')

Or

'us,en'
Related Topic