8

First of all I have already seen this question can a magento adminhtml field depend on more then one field or value? It talks about System/Configuration fields, which is not what I am looking for.

I am trying to create a form in the magento backend. I have a dropdown Dropdown with values 1, 2 and 3. I need the field X to be displayed when I select 1 or 2. How do I do this ?

I am able to display X depending on a single value of the dropdown, not for multiple values.

This is how I have done:

$this->setChild('form_after',$this->getLayout()->createBlock('adminhtml/widget_form_element_dependence')
            ->addFieldMap($X->getHtmlId(), $Xl->getName())
            ->addFieldMap($dropdown->getHtmlId(), $dropdown->getName())
            ->addFieldDependence($X->getName(), $dropdown->getName(), 1)
);

Where $x and $dropdown are variables which stores addField() result

Community
  • 1
  • 1
Hashid Hameed
  • 878
  • 1
  • 8
  • 23
  • 2
    Did you try with the last parameter of `addFieldDependence` set to an array? I mean `->addFieldDependence($X->getName(), $dropdown->getName(), array(1,2))`. – Marius Apr 14 '14 at 10:33
  • 1
    Thanks for replying Marius. I tried it now, its not working. Its not showing the field at all. – Hashid Hameed Apr 14 '14 at 17:10

2 Answers2

13

You can.

More Fields:
Just add more dependency:

->addFieldDependence($X->getName(), $dropdown_1->getName(), $value_dw_1)
->addFieldDependence($X->getName(), $dropdown_2->getName(),$value_dw_2)

More Values (same field):
You should pass an array of of values:

->addFieldDependence($X->getName(), $dropdown->getName(), array($value1,value2))

if $value1/$value2 are numbers it is better you cast them to string or it could not work properly:

->addFieldDependence($X->getName(), $dropdown->getName(), array((string)$value1,(string)value2))

There reason for this issue can be tracked down in js/mage/adminhtml/form.js in the method trackChange at one point you see this code :

...
// define whether the target should show up
    var shouldShowUp = true;
    for (var idFrom in valuesFrom) {
        var from = $(idFrom);
        if (valuesFrom[idFrom] instanceof Array) {
            if (!from || valuesFrom[idFrom].indexOf(from.value) == -1) {
                shouldShowUp = false;
            }
        } else {
            if (!from || from.value != valuesFrom[idFrom]) {
                shouldShowUp = false;
            }
        }
    }
....

you see that in case valuesFrom[idFrom] it is used indexOf to check if show the field or not, this cause problem because, it think, indexOf do a comparison taking care of the type and from.value it contains a string while in the array valuesFrom[idFrom] we have an array of numbers ...

This issue not occur in case of a single value because from.value != valuesFrom[idFrom] is not taking care of the type

WonderLand
  • 5,494
  • 7
  • 57
  • 76
-1

Simple fix for this would be to use jquery in admin section.

  1. Include jquery file in your admin section, you do this by including this in layout file

<reference name = "head">
  <action method="addJs"><script>jquery/jquery.js</script></action>
  <action method="addJs"><script>jquery/jquery.noconflict.js</script></action>
</reference>
  1. Then in the admin edit form code, you check on click:
$fieldset->addField('market_days', 'multiselect', array(
                            'label'     => Mage::helper('marketmanagement')->__('Select Days'),
                            'class'     => 'required-entry market-days',
                            'required'  => true,
                            'name'      => 'market_days',
                            'onclick' => ""//jquery code here
));

You can write jquery code to show/hide fields based on the value selected.

Hope this helps

Ashish Raj
  • 488
  • 9
  • 22
Rojan Shrestha
  • 212
  • 4
  • 7