7

I cant set any default value for textfield or select in my custom admin new ui form. In edit form it works. I want to set system config values as default values.

Here is my Dataprovider.php

public function getData(){

if (isset($this->loadedData)) {
    return $this->loadedData;
}

if ($itemId)
    $items = $this->collection->getItems();

$data = $this->dataPersistor->get('form_item');
if (!empty($data)) {
   //Do something
} else {
   //When form init without any data
   // I want to setData for some field here
}

return $this->loadedData;
}

Here is my form.xml

<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">

    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
             <item name="provider" xsi:type="string">vendor_newform.vendor_newform_data_source</item>
             <item name="deps" xsi:type="string">vendor_newform.vendor_newform_data_source</item>
        </item>
        <item name="label" xsi:type="string" translate="true">Vendor Form</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="namespace" xsi:type="string">vendor_newform</item>
    </item>
        <item name="template" xsi:type="string">templates/form/collapsible</item>
        <item name="buttons" xsi:type="array">
            <item name="save" xsi:type="string">Company\Module\Block\Adminhtml\Vendor\Edit\Button\Save</item>
            <item name="delete" xsi:type="string">Company\Module\Block\Adminhtml\Vendor\Edit\Button\Delete</item>
            <item name="back" xsi:type="string">Company\Module\Block\Adminhtml\Vendor\Edit\Button\Back</item>        
        </item>
    </argument>

    <dataSource name="vendor_newform_data_source">
         <argument name="dataProvider" xsi:type="configurableObject">
             <argument name="class" xsi:type="string">Company\Module\Model\ResourceModel\Store\DataProvider</argument>
             <argument name="name" xsi:type="string">vendor_newform_data_source</argument>
             <argument name="primaryFieldName" xsi:type="string">store_details_id</argument>
             <argument name="requestFieldName" xsi:type="string">id</argument>
             <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="submit_url" xsi:type="url" path="module/custom/save"/>
                </item>
            </argument>
         </argument>
         <argument name="data" xsi:type="array">
             <item name="js_config" xsi:type="array">
                 <item name="component" xsi:type="string">Magento_Ui/js/form/provider</item>
             </item>
         </argument>
    </dataSource>

    <fieldset name="base_fieldset">
     <argument name="data" xsi:type="array">
         <item name="config" xsi:type="array">
             <item name="label" xsi:type="string" translate="true">Details</item>
             <item name="collapsible" xsi:type="boolean">false</item>
             <item name="sortOrder" xsi:type="number">10</item>
             <item name="openOnShow" xsi:type="boolean">true</item>
         </item>
     </argument>

    <field name="store_details_id">
         <argument name="data" xsi:type="array">
             <item name="config" xsi:type="array">
                 <item name="visible" xsi:type="boolean">false</item>
                 <item name="dataType" xsi:type="string">text</item>
                 <item name="formElement" xsi:type="string">input</item>
                 <item name="source" xsi:type="string">store_details_id</item>
             </item>
         </argument>
    </field>

    <field name="store_name">
         <argument name="data" xsi:type="array">
             <item name="config" xsi:type="array">
                 <item name="label" xsi:type="string">Store Name</item>
                 <item name="visible" xsi:type="boolean">true</item>
                 <item name="dataType" xsi:type="string">text</item>
                 <item name="formElement" xsi:type="string">input</item>
                 <item name="source" xsi:type="string">store_name</item>
                 <item name="required" xsi:type="boolean">true</item>
                 <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                 </item>
             </item>
         </argument>
     </field>
    <field name="store_url">
         <argument name="data" xsi:type="array">
             <item name="config" xsi:type="array">
                 <item name="label" xsi:type="string">Store URL</item>
                 <item name="visible" xsi:type="boolean">true</item>
                 <item name="dataType" xsi:type="string">text</item>
                 <item name="formElement" xsi:type="string">input</item>
                 <item name="source" xsi:type="string">store_url</item>
                 <item name="required" xsi:type="boolean">true</item>
                 <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">true</item>
                 </item>                

             </item>
         </argument>
    </field>

        <field name="support_phone">
             <argument name="data" xsi:type="array">
                 <item name="config" xsi:type="array">
                     <item name="label" xsi:type="string">Phone</item>
                     <item name="visible" xsi:type="boolean">true</item>
                     <item name="dataType" xsi:type="string">text</item>
                     <item name="formElement" xsi:type="string">input</item>
                     <item name="source" xsi:type="string">support_phone</item>
                     <item name="required" xsi:type="boolean">true</item>
                     <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">true</item>
                     </item>                
                 </item>
             </argument>
        </field>    

    </fieldset>

</form>
  • please share your ui xml code – Rakesh Jesadiya Dec 22 '16 at 7:17
  • do you have got the solution for set system config values as default values? Please help... – Ashish Raj Apr 17 '18 at 13:01
6

You need to add below code:

<item name="default" xsi:type="string">1</item>

For reference you can check category_form as below

    <field name="is_active">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="sortOrder" xsi:type="number">10</item>
                    <item name="dataType" xsi:type="string">boolean</item>
                    <item name="formElement" xsi:type="string">checkbox</item>
                    <item name="source" xsi:type="string">category</item>
                    <item name="prefer" xsi:type="string">toggle</item>
                    <item name="label" xsi:type="string" translate="true">Enable Category</item>
                    <item name="valueMap" xsi:type="array">
                        <item name="true" xsi:type="string">1</item>
                        <item name="false" xsi:type="string">0</item>
                    </item>
                    <item name="validation" xsi:type="array">
                        <item name="required-entry" xsi:type="boolean">false</item>
                    </item>
                    <item name="default" xsi:type="string">1</item>
                </item>
            </argument>
        </field>
  • Thanks @ Prashant .It's working. I want this default value from system config. – Jancy Abraham Dec 22 '16 at 8:03
  • @JancyAbraham you find any solution ? – Ashar Riaz Apr 7 '17 at 10:43
  • No,I didn't get any solution for this – Jancy Abraham Apr 7 '17 at 10:48
  • 1
    work perfect!!! – jruzafa Sep 7 '18 at 9:41
  • 1
    Can we dynamically set a default value? – user00247 Nov 28 '18 at 8:49

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.