2

I need to add mutiple select in form. I have try this:

$fieldset->addField(
            'shipping_method_no_free',
            'select',
            [
                'options' => ['1' => __('Shipping method no free 1'), '0' => __('Shipping method no free 2')],
                'name' => 'shipping_method_no_free[]',
                'label' => __('Shipping Methods No Free'),
                'title' => __('Shipping Methods No Free'),
                'class' => 'main_acount',
                'multiple' => 'true'
            ]
        );

But it is render like normal select.

I have also try this:

$fieldset->addField(
        'shipping_method_no_free',
        'multiselect',
        [
            'options' => ['1' => __('Shipping method no free 1'), '0' => __('Shipping method no free 2')],
            'name' => 'shipping_method_no_free[]',
            'label' => __('Shipping Methods No Free'),
            'title' => __('Shipping Methods No Free'),
            'class' => 'main_acount'
        ]
    );

It render as i multiple select, but there are no content in it. enter image description here

I have also try this:

 $fieldset->addField(
        'shipping_method_no_free',
        'multiselect',
        [
            'values' => ['1' => __('Shipping method no free 1'), '0' => __('Shipping method no free 2')],
            'name' => 'shipping_method_no_free[]',
            'label' => __('Shipping Methods No Free'),
            'title' => __('Shipping Methods No Free'),
            'class' => 'main_acount'
        ]
    );

But it is render the page incompletly.

enter image description here

Is anyone facing the problem like that ?

Please, help me!

2 Answers 2

5

The last one is correct form.

 $fieldset->addField(
    'shipping_method_no_free',
    'multiselect',
    [
        'values' => ['1' => __('Shipping method no free 1'), '0' => __('Shipping method no free 2')],
        'name' => 'shipping_method_no_free[]',
        'label' => __('Shipping Methods No Free'),
        'title' => __('Shipping Methods No Free'),
        'class' => 'main_acount'
    ]
);

But it didn't render correctly, because i assigned the wrong value for values property. Change code like the following and you will be good to go:

 $fieldset->addField(
        'shipping_method_no_free',
        'multiselect',
        [
            'name' => 'shipping_method_no_free[]',
            'label' => __('Shipping Methods No Free'),
            'title' => __('Shipping Methods No Free'),
            'class' => 'main_acount',
            'values' => [
                ['label' => __('Shipping Methods No Free 0'), 'value' => 0],
                ['label' => __('Shipping Methods No Free 1'), 'value' => 1],
                ['label' => __('Shipping Methods No Free 1'), 'value' => 2],
                ['label' => __('Shipping Methods No Free 1'), 'value' => 3]
            ]
        ]
    );
1
  • hello @tam, how to render the values dynamically? instead of giving mannually May 27, 2019 at 11:23
-1

You can try this:

$fieldset->addField(
    'shipping_method_no_free',
    'multiselect',
    [
        'values' => ['1' => __('Shipping method no free 1'), '0' => __('Shipping method no free 2')],
        'name' => 'shipping_method_no_free[]',
        'label' => __('Shipping Methods No Free'),
        'title' => __('Shipping Methods No Free'),
        'class' => 'main_acount'

    ]
);

Explain: If you're using multiselect, you should use values instead of option

Ref: https://github.com/magento/magento2/blob/6d47e72c57025a8dd2bea8fef196f8397233c9c8/app/code/Magento/Widget/Block/Adminhtml/Widget/Instance/Edit/Tab/Main.php

8
  • I have tried that also, but the page is not render the right way. Click here to see the image: prntscr.com/g3pab3
    – Toan Tam
    Aug 3, 2017 at 3:04
  • Clear cache or static deploy don't help?
    – PY Yick
    Aug 3, 2017 at 3:09
  • No, it is still not work
    – Toan Tam
    Aug 3, 2017 at 3:21
  • Check exception log, any result?
    – PY Yick
    Aug 3, 2017 at 3:23
  • There are no exception thrown. It is just system.log messge
    – Toan Tam
    Aug 3, 2017 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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