SKumar SKumar - 15 days ago 9
PHP Question

Creating dependency between admin form fields in magento does not work

This is an admin form in which I have added a custom tab in order add some custom fields to it. The form works fine. but I need to add

field dependency
for some of my fields. i e

if the field
zipbasedprice_isrange
is set to
yes
, then I need to show other two fields & if it is set to
no
, then only one field should be shown
.

How can I implement this using below form?

Field dependencies should be between
zipbasedprice_isrange
,
zipbasedprice_zip
,
zipbasedprice_zip_from_zip
&
zipbasedprice_zip_to_zip
.

Please guide me.

public function getFormHtml() {
$form = new Varien_Data_Form(
array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post')
);

$this->getLayout()->createBlock('adminhtml/widget_form_renderer_element').
$this->getLayout()->createBlock('adminhtml/widget_form_renderer_fieldset').
$this->getLayout()->createBlock('adminhtml/widget_form_renderer_fieldset_element');


$fieldset = $form->addFieldset('zipbasedprice_fields', array('legend' => Mage::helper('zipbasedprice')->__('Zip Based Price'))
);

$default_country = array('label' => 'IN', 'code' => 'India');

$fieldset->addField('zipbasedprice_country', 'select', array(
'name' => 'zipbasedprice_country',
'label' => Mage::helper('zipbasedprice')->__('Country'),
'values' => Mage::getModel('adminhtml/system_config_source_country')->toOptionArray(),
'required' => true,
'style' => 'width:275px',
'value' => $default_country,
'after_element_html' => '<p class="zipbased_comment" style="margin: 0 150px; padding: 3px;"><img style="margin-right: 4px;" src="http://zonepricing.innoexts.com/skin/adminhtml/default/default/images/note_bg.gif" /><small>select the country to apply price</small></p>',
));

$regions = array();
$regions['*'] = '*';
$regionList = Mage::getModel('directory/region')->getResourceCollection()->addCountryFilter('IN')->load();
foreach($regionList as $region){ $regions[$region['code']] = $region['default_name']; }

$fieldset->addField('zipbasedprice_state', 'select', array(
'name' => 'zipbasedprice_state',
'label' => Mage::helper('zipbasedprice')->__('Region/State'),
'values' => $regions,
'required' => true,
'style' => 'width:275px',
));


$isRange = $fieldset->addField('zipbasedprice_isrange', 'select', array(
'name' => 'zipbasedprice_isrange',
'label' => Mage::helper('zipbasedprice')->__('Is Range?'),
'values' => array(
array(
'value' => false,
'label' => Mage::helper('zipbasedprice')->__('No'),
),
array(
'value' => true,
'label' => Mage::helper('zipbasedprice')->__('Yes'),
)
),
'value' => false,
'onchange' => 'onIsZipRangeChange()',
'required' => false,
'style' => 'width:275px',
));


$fieldset->addField('zipbasedprice_zip', 'text', array(
'name' => 'zipbasedprice_zip',
'label' => Mage::helper('zipbasedprice')->__('Zip Code'),
'class' => 'input',
'required' => true,
'style' => 'width:268px',
'value' => '*',
'maxlength' => 6,
));


$fieldset->addField('zipbasedprice_zip_from_zip', 'text', array(
'name' => 'zipbasedprice_zip_from_zip',
'label' => Mage::helper('zipbasedprice')->__('Zip Code From'),
'class' => 'input',
'required' => true,
'style' => 'width:268px',
'value' => '*',
'maxlength' => 6,
));

$fieldset->addField('zipbasedprice_zip_to_zip', 'text', array(
'name' => 'zipbasedprice_zip_to_zip',
'label' => Mage::helper('zipbasedprice')->__('Zip Code To'),
'class' => 'input',
'required' => true,
'style' => 'width:268px',
'value' => '*',
'maxlength' => 6,
));

$fieldset->addField('zipbasedprice_price', 'text', array(
'name' => 'zipbasedprice_price',
'label' => Mage::helper('zipbasedprice')->__('Price'),
'class' => 'input',
'required' => true,
'style' => 'width:268px',
'value' => '0.00',
));

$fieldset->addField('zipbasedprice_apply', 'select', array(
'label' => Mage::helper('zipbasedprice')->__('Apply'),
'name' => 'zipbasedprice_apply',
'required' => false,
'values' => array(
array(
'value' => 'fixed',
'label' => Mage::helper('zipbasedprice')->__('Fixed'),
),
array(
'value' => 'percentage',
'label' => Mage::helper('zipbasedprice')->__('Percentage'),
)
),
'required' => 1,
'value' => 1,
'style' => 'width:275px',
));

return $form->toHtml();
}

Answer

There are different method to do this. Easiest and simplest method would be add an onclick property to the field to which another field depend upon. ie

$fieldset->addField('zipbasedprice_isrange', 'select', array(
    'name' => 'zipbasedprice_isrange',
    'label' => Mage::helper('zipbasedprice')->__('Is Range?'),
    ......
    'onclick' => someFunction();

and then define that someFunction() to achieve what you need.

Another alternative solution is shown in this thread