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?
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?
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')],
];
}
}
Myvalues.php
to get brands options.
Commented
Aug 29, 2017 at 4:15
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
ArrayInterface
is interface which extends OptionSourceInterface
with toOptionArray()
method
Commented
Aug 14, 2018 at 5:27