{"id":158,"date":"2019-08-24T11:40:54","date_gmt":"2019-08-24T11:40:54","guid":{"rendered":"https:\/\/magebrew.com\/blog\/?p=158"},"modified":"2019-08-24T11:42:51","modified_gmt":"2019-08-24T11:42:51","slug":"magento-2-dependent-fields-in-ui-component-form","status":"publish","type":"post","link":"https:\/\/magebrew.com\/blog\/magento-2-dependent-fields-in-ui-component-form\/","title":{"rendered":"Magento 2 dependent fields in UI component form"},"content":{"rendered":"\n<p>There are numerous tutorials about how to make one field dependent on other field value in UI Form. Many of them tells you to write some JS logic. However in most cases it is enough to add <code>switcherConfig<\/code> to form declaration xml.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Let&#8217;s check how <code>switcherConfig<\/code> works on Admin Category edit form. <\/p>\n\n\n\n<p>In our custom module we create <code>app\/code\/Magebrew\/DependentFieldsExample\/view\/adminhtml\/ui_component\/category_form.xml<\/code> and add three fields: Source, Target 1, Target 2.  Then add <code>switcherConfig<\/code> to Source field with rules to show Target 1, disable Target 2 if Source value is <code>false<\/code> and hide Target 1, enable Target 2 if Source value is <code>true<\/code>. See the code below, it is pretty self-explanatory:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;form xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Ui:etc\/ui_configuration.xsd\">\n    &lt;fieldset name=\"general\">\n        &lt;field name=\"source\" formElement=\"select\">\n            &lt;settings>\n                &lt;dataType>text&lt;\/dataType>\n                &lt;dataScope>source&lt;\/dataScope>\n                &lt;label translate=\"true\">Source&lt;\/label>\n                &lt;switcherConfig>\n                    &lt;rules>\n                        &lt;rule name=\"0\">\n                            &lt;value>0&lt;\/value>\n                            &lt;actions>\n                                &lt;action name=\"0\">\n                                    &lt;target>category_form.category_form.general.target1&lt;\/target>\n                                    &lt;callback>show&lt;\/callback>\n                                &lt;\/action>\n                                &lt;action name=\"1\">\n                                    &lt;target>category_form.category_form.general.target2&lt;\/target>\n                                    &lt;callback>disable&lt;\/callback>\n                                &lt;\/action>\n                            &lt;\/actions>\n                        &lt;\/rule>\n                        &lt;rule name=\"1\">\n                            &lt;value>1&lt;\/value>\n                            &lt;actions>\n                                &lt;action name=\"0\">\n                                    &lt;target>category_form.category_form.general.target1&lt;\/target>\n                                    &lt;callback>hide&lt;\/callback>\n                                &lt;\/action>\n                                &lt;action name=\"1\">\n                                    &lt;target>category_form.category_form.general.target2&lt;\/target>\n                                    &lt;callback>enable&lt;\/callback>\n                                &lt;\/action>\n                            &lt;\/actions>\n                        &lt;\/rule>\n                    &lt;\/rules>\n                    &lt;enabled>true&lt;\/enabled>\n                &lt;\/switcherConfig>\n            &lt;\/settings>\n            &lt;formElements>\n                &lt;select>\n                    &lt;settings>\n                        &lt;options class=\"Magento\\Config\\Model\\Config\\Source\\Yesno\"\/>\n                    &lt;\/settings>\n                &lt;\/select>\n            &lt;\/formElements>\n        &lt;\/field>\n        &lt;field name=\"target1\" formElement=\"input\">\n            &lt;settings>\n                &lt;dataType>string&lt;\/dataType>\n                &lt;label translate=\"true\">Target 1&lt;\/label>\n            &lt;\/settings>\n        &lt;\/field>\n        &lt;field name=\"target2\" formElement=\"input\">\n            &lt;settings>\n                &lt;dataType>string&lt;\/dataType>\n                &lt;label translate=\"true\">Target 2&lt;\/label>\n            &lt;\/settings>\n        &lt;\/field>\n    &lt;\/fieldset>\n&lt;\/form><\/pre>\n\n\n\n<p>Source code also can be found on <a href=\"https:\/\/github.com\/magebrew\/DependentFieldsExample\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 dependent fields in UI component form<\/p>\n","protected":false},"author":1,"featured_media":160,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[26,20,23],"_links":{"self":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/158"}],"collection":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":5,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":164,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/158\/revisions\/164"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/media\/160"}],"wp:attachment":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}