4

I am not so familiar with magento so i am facing some trouble
I am creating a form in magento admin & trying to add depended form field eg- parent field is dropdown type & name is amount_type options are flat_option & percentage_option

child field are text type having name flat_value & percentage_value

So my requirment flat_value & percentage_value are hidden at first after that if i select flat_option in amount_type then show flat_value & in select percentage_option then show percentage_option.

my tried code is

$fieldset->addField('amount_type', 'select', array(
            'name' => 'amount_type',
            'label' => Mage::helper('adminhtml')->__('Amount Type'),
            'title' => Mage::helper('adminhtml')->__('Amount Type'),
            'class' => '',
            'required' => true,
            'value'     => '1',
    'values'    => array('-1'=>'Please Select','1' => 'flat_option', '2' => 'percentage_option'),
                )
        );

$fieldset->addField('flat_value', 'text', array(
            'name' => 'flat_value',
            'label' => Mage::helper('adminhtml')->__('flat amount'),
            'title' => Mage::helper('adminhtml')->__('flat amount'),
            'class' => 'validate-number',
            'required' => true,
                )
        );

$fieldset->addField('percentage_value', 'text', array(
            'name' => 'percentage_value',
            'label' => Mage::helper('adminhtml')->__('Percentage amount'),
            'title' => Mage::helper('adminhtml')->__('Percentage amount'),
            'class' => 'validate-number validate-digits-range digits-range-0-100',
            'required' => true,
                )
        );

$this->setChild('form_after', $this->getLayout()
    ->createBlock('adminhtml/widget_form_element_dependence')
        ->addFieldMap('amount_type', 'amount_type')
        ->addFieldMap('flat_value', 'flat_value)
        ->addFieldDependence('flat_value', 'amount_type', 1)
);

$this->setChild('form_after', $this->getLayout()
    ->createBlock('adminhtml/widget_form_element_dependence')
        ->addFieldMap('amount_type', 'amount_type')
        ->addFieldMap('percentage_value', 'percentage_value')
        ->addFieldDependence('percentage_value', 'amount_type', 2)
);

Tanks in advance for help

1 Answer 1

5

Try the below code

$accountField = $fieldset->addField('amount_type', 'select', array(
            'name' => 'amount_type',
            'label' => Mage::helper('adminhtml')->__('Amount Type'),
            'title' => Mage::helper('adminhtml')->__('Amount Type'),
            'class' => '',
            'required' => true,
            'value'     => '1',
    'values'    => array('-1'=>'Please Select','1' => 'flat_option', '2' => 'percentage_option'),
                )
        );

$flatField = $fieldset->addField('flat_value', 'text', array(
            'name' => 'flat_value',
            'label' => Mage::helper('adminhtml')->__('flat amount'),
            'title' => Mage::helper('adminhtml')->__('flat amount'),
            'class' => 'validate-number',
            'required' => true,
                )
        );

$presentageField = $fieldset->addField('percentage_value', 'text', array(
            'name' => 'percentage_value',
            'label' => Mage::helper('adminhtml')->__('Percentage amount'),
            'title' => Mage::helper('adminhtml')->__('Percentage amount'),
            'class' => 'validate-number validate-digits-range digits-range-0-100',
            'required' => true,
                )
        );

$this->setChild('form_after', $this->getLayout()
    ->createBlock('adminhtml/widget_form_element_dependence')
        ->addFieldMap($accountField->getHtmlId(), $accountField->getName())
        ->addFieldMap($flatField->getHtmlId(), $flatField->getName())
        ->addFieldMap($presentageField->getHtmlId(), $presentageField->getName())
         ->addFieldDependence(
                $presentageField->getName(),
                $accountField->getName(),
                2
            )
            ->addFieldDependence(
                $flatField->getName(),
                $accountField->getName(),
                1
            )
);

Refer this Link you will get better understanding of field dependency

2

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.