Hello Magento Friends,

In today’s blog, I will explain How to Add a Region(select or input) Field Based on Country Selection in the Admin Form Using UI-Component in Magento 2.

Adding a region field that adapts based on the country selection in Magento 2 is a useful feature, especially when working with forms for address or location information. It allows for a more streamlined and user-friendly experience for administrators.

Let;s get started with the steps to add a region field that dynamically updates based on the country selection using Magento 2’s UI component system.

Steps to Add Region(select or input) Field Based on Country Selection in Admin Form Using UI-Component in Magento 2:

Step 1: Create a Grid using uiComponent. For that, follow the below guide 

How to Create UI Component Grid and Form in Magento 2

Step 2: Create a file in the path given below.  

{{magento_root}}\app\code\Vendor\Extension\view\adminhtml\ui_component\your_uigrid_name.xml

Then add the code as follows

Output:

If the selected country has a state/region, it will show a dropdown field.

state dropdown field

If the selected country does not have any state/region, it will show a text field.

state text field

Conclusion:

By following these steps, you can add a region field that dynamically updates based on the country selection in your Magento 2 admin forms. If you have any doubt, share it with me through the comment section. Share the tutorial with your friends and say in touch with us.

Happy Coding!

Click to rate this post!
[Total: 1 Average: 5]