{"id":663,"date":"2019-11-08T17:04:53","date_gmt":"2019-11-08T17:04:53","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/11\/08\/use-dependency-multiple-field-in-magento-2-admin-form\/"},"modified":"2025-07-23T14:52:51","modified_gmt":"2025-07-23T09:22:51","slug":"use-dependency-multiple-field-in-magento-2-admin-form","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/use-dependency-multiple-field-in-magento-2-admin-form\/","title":{"rendered":"How To Use Dependency Multiple Field In Magento 2 Admin Form"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 platform gets better every day to improve the user experience. If the CMS falls short, the developers have the flexibility to customize the default features.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using dependency multiple fields is one such thing you can implement to improve the user experience, collect relevant and accurate data from the users, and hide the unnecessary fields in the form.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example,<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2019\/11\/Meetanshi-How-To-Use-Dependency-Multiple-Field-In-Magento-2-Admin-Form-700x474.gif\" alt=\"Method To Use Dependency Multiple Field In Magento 2 Admin \" class=\"wp-image-31423\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can also <strong>use dependency multiple field in Magento 2 admin form<\/strong> for custom requirements or custom forms. Use the below solution to create a dependable field in admin custom form in Magento2:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method To Use Dependency Multiple Field In Magento 2 Admin Form:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Block\\Adminhtml\\Extension\\Edit\\Tab;\n\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Magento\\Framework\\Data\\FormFactory;\nuse Magento\\Framework\\Registry;\nuse Magento\\Store\\Model\\System\\Store;\nuse Magento\\Backend\\Block\\Widget\\Form\\Generic;\nuse Magento\\Backend\\Block\\Widget\\Tab\\TabInterface;\nuse Magento\\Catalog\\Model\\ProductFactory;\n\nclass Detail extends Generic implements TabInterface\n{\n    protected $_systemStore;\n    protected $productFactory;\n\n    public function __construct(\n        Context $context,\n        Registry $registry,\n        FormFactory $formFactory,\n        ProductFactory $productFactory,\n        Store $systemStore,\n        array $data = []\n    )\n    {\n        $this->_systemStore = $systemStore;\n        $this->productFactory = $productFactory;\n        parent::__construct($context, $registry, $formFactory, $data);\n    }\n\n    public function getTabLabel()\n    {\n        return __('Options');\n    }\n\n    public function getTabTitle()\n    {\n        return __('Options');\n    }\n\n    public function canShowTab()\n    {\n        return true;\n    }\n\n    public function isHidden()\n    {\n        return false;\n    }\n\n    protected function _prepareForm()\n    {\n        try {\n            $model = $this->_coreRegistry->registry('current_Extension_option');\n\n            $form = $this->_formFactory->create();\n            $form->setHtmlIdPrefix('page_');\n\n            $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Settings')]);\n\n            if ($model->getId()) {\n                $fieldset->addField('id', 'hidden', ['name' => 'id']);\n            }\n\n            \n\n            $repeatPayment = $fieldset->addField(\n                'repeat_payment',\n                'select',\n                [\n                    'name' => 'repeat_payment',\n                    'label' => __('Repeat Payments'),\n                    'title' => __('Repeat Payments'),\n                    'required' => true,\n                    'options' => ['1' => __('Daily'),\n                        '2' => __('Weekly'),\n                        '3' => __('Monthly'),\n                        '4' => __('Every...'),\n                    ]\n                ]\n            );\n\n            $frequeancyOption = $fieldset->addField(\n                'frequancy_option',\n                'select',\n                [\n                    'name' => 'frequancy_option',\n                    'label' => __(''),\n                    'title' => __(''),\n                    'required' => true,\n                    'options' => $this->frequeancyOption()\n                ]\n            );\n\n            \n\n            $form->setValues($model->getData());\n            $this->setForm($form);\n\n            $this->setChild(\n                'form_after',\n                $this->getLayout()->createBlock('\\Magento\\Backend\\Block\\Widget\\Form\\Element\\Dependence')\n                    ->addFieldMap($repeatPayment->getHtmlId(), $repeatPayment->getName())\n                    ->addFieldMap($frequeancyOption->getHtmlId(), $frequeancyOption->getName())\n                    ->addFieldDependence($frequeancyOption->getName(), $repeatPayment->getName(), 4)\n            );\n\n        } catch (\\Exception $e) {\n            \\Magento\\Framework\\App\\ObjectManager::getInstance()->get('Psr\\Log\\LoggerInterface')->info($e->getMessage());\n        }\n        return parent::_prepareForm();\n    }\n\n    protected function _isAllowedAction($resourceId)\n    {\n        return $this->_authorization->isAllowed($resourceId);\n    }\n\n    protected function frequeancyOption()\n    {\n        $opt = ['1' => __('1st'),\n            '2' => __('2nd'),\n            '3' => __('3rd'),\n        ];\n\n        for ($i = 4; $i &lt;= 365; $i++) {\n            $opt[$i] = $i.\"th\";\n        }\n\n        return $opt;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 platform gets better every day to improve the user experience. If the CMS falls short, the developers have the flexibility to customize the&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-663","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/663","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=663"}],"version-history":[{"count":6,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/663\/revisions"}],"predecessor-version":[{"id":19233,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/663\/revisions\/19233"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}