I have a custom entity in my magento 2 install.
And one of the fields in this entity is of type multiselect and contains the list of all countries.
I'm using the ui-components for my admin form.
Since there are about 200 records in the select, I don't want to have a multiselect field because it's not that easy to use.
So I created one of those fancy multiselects similar to the categories field in the add/edit product admin section.
It looks nicer, but I cannot set a default value to it.
Here is my configuration (notice the default
config item):
<field name="affected_countries" formElement="select" component="Magento_Ui/js/form/element/ui-select" sortOrder="100">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">article</item>
<item name="filterOptions" xsi:type="boolean">true</item>
<item name="chipsEnabled" xsi:type="boolean">true</item>
<item name="disableLabel" xsi:type="boolean">true</item>
<item name="default" xsi:type="string">RO,MD</item>
</item>
</argument>
<settings>
<elementTmpl>ui/grid/filters/elements/ui-select</elementTmpl>
<dataType>text</dataType>
<label translate="true">Affected Countries</label>
<dataScope>affected_countries</dataScope>
<componentType>field</componentType>
</settings>
<formElements>
<select>
<settings>
<options class="Magento\Config\Model\Config\Source\Locale\Country"/>
</settings>
</select>
</formElements>
</field>
And I'm expecting the 2 values I placed in the default field to be selected:
If I turn the element into a simple multiselect it works nicely.
<field name="affected_countries" formElement="multiselect" sortOrder="100">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">article</item>
<item name="default" xsi:type="string">RO,MD</item>
</item>
</argument>
<settings>
<dataType>text</dataType>
<label translate="true">Affected Countries</label>
<dataScope>affected_countries</dataScope>
</settings>
<formElements>
<multiselect>
<settings>
<options class="Magento\Config\Model\Config\Source\Locale\Country"/>
</settings>
</multiselect>
</formElements>
</field>
I tied with this format for the default
setting
<item name="default" xsi:type="string">RO,MD</item>
and this one also:
<item name="default" xsi:type="array">
<item name="MD" xsi:type="string">MD</item>
<item name="RO" xsi:type="string">RO</item>
</item>
Also tried with the tag select
and multiselect
inside the formElements
tag.
All of my tries ended up in failure.
Using the default
setting in any other types of fields, as instructed here (text, select, date, ...) works nicely.
Any suggestion for the fancy selects? Something I missed?
Note: I know I can supply a default value in the data provider that populates the form, but I'm trying to avoid this as it looks ugly and it's not that extensible and not consistent with the rest of the fields.
<options class="Magento\Config\Model\Config\Source\Locale\Country"/>
. You need a similar class that implements\Magento\Framework\Option\ArrayInterface
and has a method calledtoOptionArray
that returns an array with your values. each element from the array must look like this['value' => ..., 'label' => ...]