How To

Magento 2: Add Readonly Field in ui_component Form

Hello Magento Friends,

Today’s guide is about making the UI-component form field read-only in Magento 2.

UI component grid and form are very important in Magento 2. You can set write protection in the UI_component form field. Let’s see How to Add Readonly Field in the ui_component Form in Magento 2.

Steps to Add Readonly Field in ui_component Form in Magento 2:

Step 1: First create a ui_component file

For that use this guide – How to Create UI Component Grid and Form in Magento 2

Step 2: Now move to the below path

app/code/Vendor/Extension/view/adminhtml/ui_component/uiexample_form.xml

Edit the code snippet as follows

<?xml version="1.0" encoding="UTF-8"?>
<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">uiexample_form.grid_record_grid_list_data_source</item>
            <item name="deps" xsi:type="string">uiexample_form.grid_record_grid_list_data_source</item>
         </item>
        <item name="label" xsi:type="string" translate="true">Add New Record</item>
        <item name="config" xsi:type="array">
            <item name="dataScope" xsi:type="string">data</item>
            <item name="namespace" xsi:type="string">uiexample_form</item> 
        </item> 
        <item name="template" xsi:type="string">templates/form/collapsible</item> 
        <item name="buttons" xsi:type="array"> 
            <item name="save" xsi:type="string">Vendor\Extension\Block\Adminhtml\Edit\Save</item> 
        </item> 
    </argument> 
    <dataSource name="grid_record_grid_list_data_source"> 
        <argument name="dataProvider" xsi:type="configurableObject"> 
            <argument name="class" xsi:type="string">Vendor\Extension\Model\DataProvider</argument> 
            <argument name="name" xsi:type="string">grid_record_grid_list_data_source</argument> 
            <argument name="primaryFieldName" xsi:type="string">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="*/*/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="general_information"> 
        <argument name="data" xsi:type="array"> 
            <item name="config" xsi:type="array"> 
                <item name="collapsible" xsi:type="boolean">false</item> 
                <item name="label" xsi:type="string" translate="true">General Information</item> 
                <item name="sortOrder" xsi:type="number">20</item> 
            </item> 
        </argument> 
        <field name="name" sortOrder="10" formElement="input"> 
            <settings> 
                <dataType>text</dataType>                 <visible>true</visible>
 
                <label translate="true">Name</label> 
            </settings> 
        </field>
        <field name="identifier" formElement="hidden">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="dataType" xsi:type="string">text</item>
                        <item name="label" xsi:type="string" translate="true">Country</item>
                        <item name="formElement" xsi:type="string">input</item>
                        <item name="source" xsi:type="string">identifier</item>
                        <item name="dataScope" xsi:type="string">identifier</item>
                        <item name="default" xsi:type="string">India</item> 
                    </item>
                </argument>

                <!-- Add Below Code To Make Read Only  Field-->
                <settings>
                    <dataType>text</dataType>
                    <disabled>1</disabled>
                </settings>
                <!--  -->

            </field>

        <field name="comment" sortOrder="20" formElement="input"> 
            <settings> 
                <dataType>text</dataType> 
                <visible>true</visible> 
                <label translate="true">Comment</label>
             </settings>
 
        </field> 
        <field name="address" sortOrder="20" formElement="input"> 
            <settings> 
                <dataType>text</dataType> 
                <visible>true</visible> 
                <label translate="true">Address</label> 
            </settings> 
        </field> 
    </fieldset> 
</form>

You have to add “<settings>” and “<disabled>1</disabled>” to make any field read-only. As you can see in the screenshot below, the country field is read only.

Conclusion:

This way you can make any field read-only in the ui_component form in Magento 2. If you face any difficulty, freely connect with me through the comment section. Share the article further and stay updated with us.

Happy Coding!

Click to rate this post!
[Total: 3 Average: 3.7]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago