Back to Top

Create a dependant field in admin configuration in magento 2

Updated 26 March 2024

Let’s create a dependant field in admin configuration in magento 2.

In this article we will learn how to create a dependant field in admin configuration in magento 2.

So, the file which is used to define configurations in admin is system.xml. I  suppose that you know where this file is located in a custom module.

So, here is the code in which, on select a option with value “1” from a drop-down list a text field (dependson) will appear :

<!-- this is a drop-down field which contains options -->
<field id="custom" translate="label comment" sortOrder="1" type="select" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Please Select a Value : </label>
    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
    <!-- source file contains the array of options of drop-down list -->
</field>
<!-- this is a text field which depends on the option value -->
<field id="dependson" translate="label comment" sortOrder="2" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
    <label>Enter the Name : </label>
    <depends>
        <field id="custom">1</field>
    </depends>
</field>

And if you want to create a field dependant on two or more values, use the below code :

Searching for an experienced
Magento 2 Company ?
Find out More
<depends>
    <field id="custom" separator=",">0,1</field>
</depends>

In above code if you select options with values “0” or “1” from drop-down list then the text- field will be appeared in the admin configuration.

That’s all in this article, hope it will help you to add a dependant field in admin configuration. Try the above code and if you have any issue just comment below. 🙂

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


1 comments

  • Ali Majidian
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home