2

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?

  • Hello, Your code created a multiselect field. I'm trying to get all brands options in multiselect options. – Vinaya Maheshwari Aug 29 '17 at 3:50
6

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')],
        ];
    }
}
  • Thanks for your post, I have updated Myvalues.php to get brands options. – Vinaya Maheshwari Aug 29 '17 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(). – Prince Patel Aug 29 '17 at 4:31
  • Yes I updated code. – Vinaya Maheshwari Aug 29 '17 at 4:38
  • I want to ask what inside \Magento\Framework\Option\ArrayInterface ? – fudu Aug 14 '18 at 4:35
  • 1
    @fudu ArrayInterface is interface which extends OptionSourceInterface with toOptionArray() method – Prince Patel Aug 14 '18 at 5:27

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.