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

4

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

  • tried this on Magento ver. 1.9.3.10 didn't work at all but perhaps it's our cms doing something bad – OZZIE Apr 25 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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