2

In Magento 2 if we need hide/show the field in the same group we will fallow as below.

<section id="custom_general" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
     <group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>General Options</label>
                    <field id="active" translate="label" type="select" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Enabled</label>
                        <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                        <comment>Enable/Disable</comment>
                    </field>                
                    <field id="depends_on_file" translate="File Name"  sortOrder="5" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Enter the File Name: </label>
                        <depends>
                            <field id="active">1</field>
                        </depends>
                    </field>              
                </group>
</section>

If we use <depends> it will act like show/hide based on active field.

In my case fields are in the different tab sections and I used the same <depends>, but not working for me. For this I used below method.

<section id="custom_customer" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
             <group id="custom_customer_general" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>General</label>
                 <field id="file_name" translate="File Name"  sortOrder="5" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Enter the File Name: </label>
                    <depends>
                        <field id="custom_general/general/active">1</field>
                    </depends>
                </field> 
            </group>           
        </section>

I used the <field id="custom_general/general/active">1</field> validation to show/hide, but it's not working.

Can you please suggest me if I went wrong?

1 Answer 1

3

Try this

<section id="custom_customer" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
     <group id="custom_customer_general" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
        <label>General</label>
         <field id="file_name" translate="File Name"  sortOrder="5" type="text" showInDefault="1" showInWebsite="1" showInStore="1">
            <label>Enter the File Name: </label>
            <depends>
                <field id="*/*/active">1</field>
            </depends>
        </field> 
    </group>           
</section>

add this file set value

etc/config.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <custom_customer>
            <general>
                <active>0</enable>     
            </general>
        </custom_customer>
    </default>
</config>
1
  • Is this work dynamically? when we switched tab.
    – Bojjaiah
    Nov 10, 2020 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.