17

So you can set in the system.xml file of a module a dependency between fields by adding a <depends> tag in one of the fields.

<field1>
    ....
</field1>
<field2>
    ....
    <depends>
       <field1>1</field1>
    </depends>
</field2>

The code above means that field2 will be shown when the value of field1 is 1. I want to know how/if can I tell Magento to show field2 if the value for field1 is 1 OR 2?

3 Answers 3

32

Try this:

<depends>
    <field separator="|">
        <value>1|2|3</value>
    </field>
</depends>
4
  • Do you know that this works, or are you just putting it out there?
    – benmarks
    Feb 17, 2014 at 0:04
  • 1
    Dude...I don't know how, but this actually works. Now I'm ashamed and sorry for all my bad words I said to the guys that implemented the config section. For some reason $dependent['separator'] returns the value of the attribute separator. To whom ever downvoted this, please upvote (twice if possible). Works perfectly. Thanks. cc @benmarks
    – Marius
    Feb 17, 2014 at 7:16
  • 1
    This working != it being a good idea. I think there's no need to apologize for your words :-D
    – benmarks
    Feb 17, 2014 at 13:27
  • verrrrrry cool! This appears to also be working in Magento 2.
    – sdaugherty
    Aug 22, 2021 at 3:50
7

[EDIT]

I was wrong in my answer below. I will not delete it (yet) because I got 7 upvotes on this :). But I'm editing it so you all have the chance to retract your vote (even downvote it, because I deserve it).

Original Answer

Ha!..I found it.
Short answer: You cannot!
Long answer: You should be able to do it if someone would have known the difference between an array and an object.
In theory this should work

<field1>
    ....
</field1>
<field2>
    ....
    <depends>
       <field1>
           <value>1|2</value>
           <separator>|</separator>
       </field1>
    </depends>
</field2>

But in the code that handles the dependency, Mage_Adminhtml_Block_System_Config_Form::initFields around line 366 there is this code

if (isset($dependent['separator'])) {
    $dependentValue = explode((string)$dependent['separator'], $dependentValue);
}

$dependent is always an object so $dependent['separator'] is never set.
If I change the code above to

if (isset($dependent->separator)) {
    $dependentValue = explode((string)$dependent->separator, $dependentValue);
}

everything works smoothly.
I guess I cannot change the core just for the sake of an extension so I have to create 2 fields instead of 1, one for each value from field1 or create a custom js that handles this and add it to the config page.

3
  • 2
    Damn it. I know this kind of bugs :-) I hope you reported it? Great insight! Oct 24, 2013 at 17:39
  • 1
    Oh man that is some great investigation are you sure you are not Sherlock? +1 for reporting it. Oct 24, 2013 at 20:07
  • 2
    @DavidManners elementary my dear watson
    – Marius
    Oct 24, 2013 at 20:30
0

I know this was asked under Magento 1 but since I landed here looking for an example for Magento 2 and the answers here pointed me in the right direction, here is a working example tested in Magento 2.4.4 in case it helps anyone else:

<field id="time" translate="label comment" type="time" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Start Time</label>
<depends>
    <field id="frequency" separator="|">D|W|M</field>
</depends>

There is also a negative parameter that can be used to tailor the condition even more. enter image description here

Your Answer

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

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