9

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?

  • 1
    hum.... PR ... hum ... – Raphael at Digital Pianism May 18 '17 at 15:25
  • @RaphaelatDigitalPianism I need a confirmation first. – Marius May 18 '17 at 15:25
  • Haven't looked too much into tbh but sound very much like someone forgot something – Raphael at Digital Pianism May 18 '17 at 15:27
  • Perhaps a bit off-topic: but will it be backward compatible? :-S – Giel Berkers May 18 '17 at 15:30
  • @GielBerkers. From what I tested so far, the old version still works on version 2.2-dev. So I would say yes. – Marius May 18 '17 at 15:33
2

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>

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.