3

I have created a custom module in magento2. And created ui component form to save title and sort_order value, now I want to add brands as multi-select field in my module's form.

How to add a multi-select field for brands attribute options?

1
  • Hello, Your code created a multiselect field. I'm trying to get all brands options in multiselect options. Commented Aug 29, 2017 at 3:50

1 Answer 1

8

Add new field in UI form

<field name="group">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">{Vendor}\{Module}\Model\Model\Source\Myvalues</item>
        <item name="config" xsi:type="array">
            <item name="dataType" xsi:type="string">text</item>
            <item name="label" translate="true" xsi:type="string">Your Label</item>
            <item name="formElement" xsi:type="string">multiselect</item>
        </item>
    </argument>
</field>

Now create Myvalues.php to provide multiselect options at

app/code/{Vendor}/{Module}/Model/Source/Myvalues.php

<?php

namespace Vendor\Module\Model\Source\Myvalues;

class Myvalues implements \Magento\Framework\Option\ArrayInterface
{
    public function toOptionArray()
    {
        return [
            ['value' => 1, 'label' => __('Test One')],
            ['value' => 2, 'label' => __('Test Two')],
            ['value' => 3, 'label' => __('Test Three')],
        ];
    }
}
10
  • Thanks for your post, I have updated Myvalues.php to get brands options. Commented Aug 29, 2017 at 4:15
  • @VinayaMaheshwari SImple assign brand collection to option. Assign app/code/Vendor/Module/Model/ResourceModel/Brand/Collection to <item name="options" xsi:type="object"> OR you can get brand collection in MyValue.php and return collection array to toOptionArray(). Commented Aug 29, 2017 at 4:31
  • Yes I updated code. Commented Aug 29, 2017 at 4:38
  • I want to ask what inside \Magento\Framework\Option\ArrayInterface ?
    – fudu
    Commented Aug 14, 2018 at 4:35
  • 1
    @fudu ArrayInterface is interface which extends OptionSourceInterface with toOptionArray() method Commented Aug 14, 2018 at 5:27

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.