Set a default config value on a ui-component form field
-
09-10-2020 - |
Question
The ui-components format for forms and grids is changing in version 2.2 (dev). (maybe it already did). How can I set a default value for a field.
Before 2.2-dev you could add a field like this in the form.
<field name="title">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="default" xsi:type="string">Some default value.</item>
<item name="label" xsi:type="string" translate="true">Page Title</item>
<item name="formElement" xsi:type="string">input</item>
<item name="source" xsi:type="string">source_here</item>
<item name="sortOrder" xsi:type="number">20</item>
<item name="dataScope" xsi:type="string">title</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
</item>
</argument>
</field>
in 2.2-dev the column looks like this:
<field name="title" sortOrder="20" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">source_here</item>
</item>
</argument>
<settings>
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
</validation>
<dataType>text</dataType>
<label translate="true">Page Title</label>
<dataScope>title</dataScope>
</settings>
</field>
Notice the default
is missing from my second example.
I tried <default>Some default value</default>
and the same syntax as for the previous version but the xml does not validate.
Is there a way to do it in the new version or someone overlooked it?
Solution
I dig some core files and I see that they have set the default value like this
Source vendor/magento/module-catalog/view/adminhtml/ui_component/category_form.xml
You can try below way May be it will help you:
<field name="title" sortOrder="20" formElement="input">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">source_here</item>
<item name="default" xsi:type="string">Some default value.</item> <!--Default Value-->
</item>
</argument>
<settings>
<validation>
<rule name="required-entry" xsi:type="boolean">true</rule>
</validation>
<dataType>text</dataType>
<label translate="true">Page Title</label>
<dataScope>title</dataScope>
</settings>
</field>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange