<?php
protected function _prepareForm()
{
$helper = Mage::helper(‘catalog');
$form = new Varien_Data_Form(array(
'id' => 'edit_form', //Id which appears on the form (html page). keep this name to avoid issues.
'action' => $this->getUrl('*/*/myProcessFile'),
'method' => 'post',
'enctype' => 'multipart/form-data'
)
);
$fieldset = $form->addFieldset('base_fieldset', array('legend' => $helper->__('Form Dependency')));
//Show field2 yes/no
$fieldset->addField('field1', 'select', array(
'name' => 'field1',
'id' => 'field1',
'label' => Mage::helper('catalog')->__('Show field 2'),
'title' => Mage::helper('catalog')->__(’Show field 2'),
'options' => Mage::getSingleton('adminhtml/system_config_source_yesno')->toArray(),
'onChange' => 'this.value = this.checked ? 1 : 0;',
'onclick' => 'clearFields1(this.value)',
'value' => '',
'after_element_html' =>
'<script type="text/javascript">
function clearFields1(myValue) {
if (myValue=="0") {
document.getElementById("field2").value = "";
document.getElementById("field3").value = "";
}
}
</script>'
)
);
//Show field3 yes/no
$fieldset->addField('field2', 'select', array(
'name' => 'field2',
'id' => 'field2',
'label' => Mage::helper('catalog')->__('Show field 3'),
'title' => Mage::helper('catalog')->__(’Show field 3'),
'options' => Mage::getSingleton('adminhtml/system_config_source_yesno')->toArray(),
'onChange' => 'this.value = this.checked ? 1 : 0;',
'onclick' => 'clearFields12(this.value)',
'value' => '',
'after_element_html' =>
'<script type="text/javascript">
function clearFields2(myValue) {
if (myValue=="0") {
document.getElementById("field3").value = "";
}
}
</script>'
)
);
//field3 yes/no
$fieldset->addField('field3', 'select', array(
'name' => 'field3',
'id' => 'field3',
'label' => Mage::helper('catalog')->__('Field 3'),
'title' => Mage::helper('catalog')->__('Field 3'),
'options' => Mage::getSingleton('adminhtml/system_config_source_yesno')->toArray(),
'onChange' => 'this.value = this.checked ? 1 : 0;',
'onclick' => 'clearFields13(this.value)',
'value' => '',
'after_element_html' =>
'<script type="text/javascript">
function clearFields3(myValue) {
if (myValue=="0") {
alert('Do something’);
}
}
</script>'
)
);
// Set form to be rendered in template
$this->setForm($form);
// Set block for the "form_after" block in template
// @see /app/design/adminhtml/default/default/template/widget/form.phtml
$this->setChild('form_after',
// The content of the "form_after" block is a Mage_Adminhtml_Block_Widget_Form_Element_Dependence block typ
$this->getLayout()->createBlock('adminhtml/widget_form_element_dependence')
/**
* Map the form fields to the dependencies block IDs
* This is kind of a "form field" => "dependencies block identifier" association
*/
// Our form's "field1" is used with the "field1_depend_id" ID in the dependencies block
->addFieldMap('field1', 'filed1_depend_id')
->addFieldMap('field2', 'field2_depend_id')
->addFieldMap('field3', 'field3_depend_id')
/**
* Create dependencies
* Each addFieldDependence() can be understood like:
* "Show this [dependencies block identifier] if this [dependencies block identifier] is [value]
*
* Defined dependencies do stack. This allows some "AND" statements.
*/
// Show 'show_child_depend_id' if 'show_children_depend_id' is '1'
// In other words, based on the previous mapping: "Show the 'show_child_field' if 'show_children_field' value is 1
->addFieldDependence('field2_depend_id', 'field1_depend_id', '1')
->addFieldDependence('field3_depend_id', 'field1_depend_id', '1')
->addFieldDependence('field3_depend_id', 'field2_depend_id', '1')
);
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
Please ensure, you need to create field1, field2 and field3 form fields.
Hope it helps
Cheers!