Create Magento Backend Field ReadOnly

Create Magento Backend Field ReadOnly

August 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Create Magento Backend Field ReadOnly

Making Magento Backend Fields Read-Only

As a senior developer with extensive experience in e-commerce platforms like Magento, I often encounter cases where clients need specific fields to be read-only within the admin panel. This is a common requirement for fields that should not be altered by regular users or admins, ensuring data integrity and consistency.

Understanding Magento Attribute Creation

Firstly, let’s break down the code provided for creating an attribute:

$installer->addAttribute("customer", "attrcode",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "label",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default"  => "",
    "frontend" => "",
    "unique"   => false,
));

This creates a new attribute named attrcode for the customer entity. However, as you correctly pointed out, this field is not created as read-only by default.

Creating Read-Only Attributes

To make an attribute read-only in Magento, you need to modify the field configuration and use the disabled option during form rendering. Here’s a step-by-step guide:

  1. Create the Attribute: You can use the provided code or create it through the admin panel (System -> Configuration -> Customer Configuration).

  2. Enable Form Fields for Admin: Go to Stores -> Settings -> Configuration. Under the Advanced tab, click on System and enable the “Form Fields Customization” option.

  3. Customize Field Options: In your module configuration file (usually etc/adminhtml/system.xml), add or modify the field options:

<field id="attrcode">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="source_model" xsi:type="string">magento\customer\Model\Attribute\Source\Text</item>
            <item name="label" xsi:type="string" translate="true">Label</item>
            <item name="name" xsi:type="string">attrcode</item>
            <item name="visible" xsi:type="boolean">true</item>
            <item name="disabled" xsi:type="boolean">true</item> <!-- Add this line -->
        </item>
    </argument>
</field>

The disabled attribute set to true prevents the field from being edited in the admin panel.

  1. Clear Cache: After making these changes, make sure to clear the cache (System -> Tools -> Cache Management).

Best Practices and Further Reading

Remember that this approach only prevents editing; it doesn’t prevent data entry during object creation or update via API. For full read-only functionality across all channels, consider using the attribute_set feature or implementing a validation rule.

For more advanced customizations and to learn about Magento’s attribute system in-depth, I recommend checking out resources like the Magento Developer Documentation and our own guides on Laravel Company.

Conclusion

Making a field read-only in Magento involves more than just creating the attribute; it requires modifying the admin form configuration. By following this guide, you can ensure that specific fields are protected from unwanted edits within your client’s backend.

Remember to always test these changes thoroughly in a staging environment before deploying them to production. As with any customization, careful consideration of potential edge cases is essential for maintaining a stable and secure e-commerce system.

Admin Configuration Customers CLI

Our network