---
title: "How to Implement Fields Dependency in Custom Admin Form of Magento 2"
url: "https://magecomp.com/blog/implement-fields-dependency-custom-admin-form-magento-2/"
date: "2019-09-27T19:40:11+00:00"
modified: "2025-02-21T06:40:47+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 396
reading_time: "2 min read"
summary: "While surfing online, you probably have filled hundreds of forms whether it is related to signup, contact or anything. If you have noticed when you select one particular option it shows other relev..."
description: "While surfing online, you probably have filled hundreds of forms whether it is related to signup, contact or&hellip;"
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Magento 2: How To Display Count Of Product with Page Title on Category Page "
    url: "https://magecomp.com/blog/magento-2-display-count-of-product-with-page-title-on-category-page/"
  - title: "How to Create Custom Order Status and State Programmatically in Magento 2?"
    url: "https://magecomp.com/blog/create-custom-order-status-state-programmatically-magento-2/"
  - title: "Magento 2: How to Apply Order Status Filter on Order History Page"
    url: "https://magecomp.com/blog/magento-2-apply-order-status-filter-on-order-history-page/"
---

# How to Implement Fields Dependency in Custom Admin Form of Magento 2

_Published: September 27, 2019_  
_Author: Dhiren Vasoya_  

![How To Implement Fields Dependency in Custom Admin Form of Magento 2](https://magecomp.com/blog/wp-content/uploads/2019/09/How-To-Implement-Fields-Dependency-in-Custom-Admin-Form-of-Magento-2-1024x332.webp)

While surfing online, you probably have filled hundreds of forms whether it is related to signup, contact or anything. If you have noticed when you select one particular option it shows other relevant options to select from. Such options are known as Field Dependencies controls. It hides unnecessary fields until you don’t make a relational choice. Using such field dependencies in the form helps you to prevent bad user experience and collect all related information in less time.

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-6.webp)](https://magecomp.com/services/hyva-theme-development/)
While creating an extension from scratch many times it happens that you have so many configurations but some of them are dependable at that time, you have to create field dependencies.
For example, here we have used a dropdown field to show or hide dependable textbox. If the user selects “show” it shows the dependable textbox else it will gets hidden.
Once you create a basic form now inside your custom extension folder. After that, you have to add below code in your **“form.php”** file.
**app\code\Vendor\Extension\Block\Adminhtml\Grid\Edit\Tab\Form.php**

```
<?php
namespace VendorExtensionBlockAdminhtmlGridEditTab;

class Form extends MagentoBackendBlockWidgetFormGeneric implements MagentoBackendBlockWidgetTabTabInterface
{
    protected function _prepareForm()
    {
        $form = $this->_formFactory->create();
        $fieldset = $form->addFieldset('base_fieldset', array('legend' => __('General')));
        $main = $fieldset->addField(
            "main",
            "select",
            [
                "label"     =>      __("Main"),
                "class"     =>      "required-entry",
                "name"      =>      "main",
                "values"    =>      [
                    ["value" => 0,"label" => __("Hide")],
                    ["value" => 1,"label" => __("Show")],
                ]
            ]
        );
        $sub = $fieldset->addField(
            "dependent_field",
            "text",
            [
                "label"     => __("Dependent Field"),
                "class"     => "required-entry",
                "required"  => true,
                "name"      => "dependent_field"
            ]
        );
        $this->setChild(
            'form_after',
            $this->getLayout()->createBlock('MagentoBackendBlockWidgetFormElementDependence')
                ->addFieldMap($main->getHtmlId(), $main->getName())
                ->addFieldMap($sub->getHtmlId(), $sub->getName())
                ->addFieldDependence($sub->getName(), $main->getName(), 1)
        );

        $this->setForm($form);
        return parent::_prepareForm();
    }
    public function getTabLabel()
    {
        return __('General');
    }
    public function getTabTitle()
    {
        return __('General');
    }
    public function canShowTab()
    {
        return true;
    }
    public function isHidden()
    {
        return false;
    }
}
```

[![Hire Magento Programmer](https://magecomp.com/blog/wp-content/uploads/2025/02/Hire-Magento-Programmer-CTA-1.webp)](https://magecomp.com/services/hire-magento-programmer/)

That’s it. You are free to manipulate this code according to your need to show hide multiple form controls.

Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issue while implementing this code.

[![Magento version upgrade](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Version-Upgrade-CTA-2-2.webp)](https://magecomp.com/services/magento-upgrade-services/)

Happy Fixing!


---

_View the original post at: [https://magecomp.com/blog/implement-fields-dependency-custom-admin-form-magento-2/](https://magecomp.com/blog/implement-fields-dependency-custom-admin-form-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-10-02 10:34:49 UTC_  
