Creating dependency between admin form fields in magento does not work

Creating dependency between admin form fields in magento does not work

August 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Creating dependency between admin form fields in magento does not work

Creating Dependency Between Admin Form Fields in Magento

Introduction

In this tutorial, we will explore how to implement field dependencies in an admin form within Magento. Field dependencies allow you to control which fields are visible or required based on the value selected in another field. This is particularly useful when you want to create complex pricing structures with varying conditions.

The Challenge

We have a custom admin form for managing zip-based pricing. The current form works fine, but we need to add dependency between certain fields:

  • If the zipbasedprice_isrange field is set to “Yes”, the two additional fields (zipbasedprice_zip_from_zip and zipbasedprice_zip_to_zip) should become visible.
  • If it’s set to “No”, only one field (zipbasedprice_zip) should be shown.

Let’s dive into implementing this functionality.

Step 1: Identify the Fields

First, let’s identify the fields we need to work with:

  • zipbasedprice_isrange: The main field that will trigger the dependency.
  • zipbasedprice_zip: The single field that is always visible.
  • zipbasedprice_zip_from_zip and zipbasedprice_zip_to_zip: The two additional fields that become visible when zipbasedprice_isrange is set to “Yes”.

Step 2: Create a Javascript Function

Magento uses Javascript to handle form dependencies. We need to create a function called onIsZipRangeChange() that will be triggered whenever the zipbasedprice_isrange field changes.

Add this function in your main Javascript file (usually located at /skin/adminhtml/default/default/js/your_module.js):

function onIsZipRangeChange() {
    var isZipRange = document.getElementById('zipbasedprice_isrange').value;

    if (isZipRange == '1') { // If Yes
        document.getElementById('zipbasedprice_zip_from_zip').style.display = 'block'; // Show the range fields
        document.getElementById('zipbasedprice_zip_to_zip').style.display = 'block';
    } else { // If No
        document.getElementById('zipbasedprice_zip_from_zip').style.display = 'none'; // Hide the range fields
        document.getElementById('zipbasedprice_zip_to_zip').style.display = 'none';
    }
}

This function uses the current value of zipbasedprice_isrange to toggle the visibility of the two range fields.

Step 3: Update the Form

Now, let’s update the form code in your admin controller. Look for the line where you create the form using $form = new Varien_Data_Form().

Add this line just after that:

$isRange->setClass('required-entry'); // Add a class to indicate required field

This adds a required-entry class to the zipbasedprice_isrange field, which is used for validation.

Step 4: Add Field IDs

For the Javascript function to work correctly, each form field needs an ID that matches its name. In your form code, add the id attribute to all fields:

'zipbasedprice_zip_from_zip' => array(
    // ...
    'id' => 'zipbasedprice_zip_from_zip',
),

Step 5: Test the Form

Save your changes and refresh the admin form. When you select “Yes” in the Is Range? dropdown, the two range fields should become visible. If you select “No”, they should disappear.

Conclusion

By following these steps, we have successfully implemented field dependencies in our Magento admin form. This allows us to create complex pricing structures with varying conditions based on user input.

Remember, the key is to use Magento’s built-in form rendering system and leverage its support for Javascript functionality. Always test your form thoroughly after making changes to ensure all fields behave as expected.

For more advanced form manipulation in Magento, you might want to explore FormLayout and Layout XML.

Happy coding!

Admin Pricing JavaScript

Our network