2

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 :D

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! )

1

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'
  • Thanks a lot for that hints, but sadly it didn't work out. I added some more info in the main question. – Peter Ukena Feb 3 '14 at 21:55
  • Take a look on a class Varien_Data_Form_Element_Multiselect, which you can find in lib folder. Try to debug it. You will get more understanding how multiselect field is created and will found what are you doing wrong. – oleksii.svarychevskyi Feb 3 '14 at 22:27
  • I know about that class - I have looked into it before. But I turned out to be an idiot and not read properly. While saving the values in my controller, I serialized that array instead of using 'type' => 'multiselect' and let Magento do that work. Now everything works as expected. Thanks a lot again for that hint :) – Peter Ukena Feb 4 '14 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.