Cascading Country-State-City Selector
Prerequisites: Getting the Project Filesβ
Before you begin, this tutorial requires you to download two key assets from our GitHub repository:
- The "Form Template" Rule Project: The foundational project for creating dynamic forms.
- The Front-End Renderer: The HTML and JavaScript files needed to display the forms.
Follow these two steps to get everything you need.
Step 1: Import the Rule Projects into Studioβ
We use a PowerShell script to automatically find and install all the sample rule projects, including the essential "Form Template," into your Corticon.js Studio.
-
Navigate to the
sample-projects
Directory: -
Download the Import Script:
- In the file list, find and click on
Import-CorticonSamples.ps1
. - On the script's page, click the Download raw file button (the icon with a downward arrow).
- Save the script to a convenient location, like your Desktop.
- In the file list, find and click on
-
Run the Script:
- Open a PowerShell window, navigate to where you saved the file, and run it:
.\Import-CorticonSamples.ps1
- This script will temporarily clone the
dynamic-forms
repository in the background, find all the samples, and install them into your Corticon.js Studio.
- Open a PowerShell window, navigate to where you saved the file, and run it:
-
Restart Corticon.js Studio:
- After the script finishes, restart the studio. Go to Help -> Samples to find the "Form Template" project.
Step 2: Get the Front-End Filesβ
The front-end rendering application is in the front-end-files
directory. We will use the downgit
tool to download just this specific folder.
-
Download the Directory:
- Click this direct link to download the
front-end-files
directory as a ZIP file: - Download
front-end-files
using downgit - This will download a file named
front-end-files.zip
.
- Click this direct link to download the
-
Unzip the Files:
- Create a main project folder on your computer for this work (e.g.,
C:\corticon-tutorial
). - Unzip the
front-end-files.zip
directly into that folder. Your folder structure should now look like this:C:\corticon-tutorial\
βββ front-end-files\
βββ clientSideComponent\
βββ decisionServices\
βββ trace\
βββ index.html
βββ ... (and other files)
- Create a main project folder on your computer for this work (e.g.,
-
Important Note for Later:
- As you proceed through the tutorials, you will generate new Decision Services from Corticon.js Studio. You must save these into the
decisionServices
subfolder. For example:C:\corticon-tutorial\front-end-files\decisionServices\
. This ensures the front-end application can find and load them.
- As you proceed through the tutorials, you will generate new Decision Services from Corticon.js Studio. You must save these into the
Tutorial: Building the Cascading Selectorβ
In this final hands-on tutorial, you will build a three-level cascading dropdown selector for Country, State, and City using the Form Template. This is a common requirement in applications that collect address information and is the definitive example of how to chain filtered, data-driven controls.
What You Will Learn:
- How to create a multi-level cascading selection form.
- How to use a user's selection in one stage to dynamically filter the
dataSource
for the next stage. - How to write more advanced JSONPath expressions to pinpoint the exact data you need from a complex JSON response.
Step 1: Building the Vocabularyβ
The data model for this form is simple, as we only need to store the final user selections.
-
Β In Corticon.js Studio, open the Form Template project you imported earlier.
-
Β In the Project Explorer, open the
Rule Vocabulary.ecore
file. -
Β You will see the standard
UI
,Container
,UIControl
, andOption
entities. We need to add one more to store our registrant's information. -
Β Expand the 'Data' folder in the rule vocabulary. Double click on the entity 'renameToYourPathToData', and enter
Location
. -
Right click on the entity called 'Data' and click 'Add Association'.
-
Β For the source entity name, keep 'Data.Data' for the the target entity name, select 'Data.Location', and click the 'One' button beneath it. Then, change 'Navigability' to 'Data.Data->Data.Location'.
-
Add the following attributes to the
Location
entity:
Attribute Name | Data Type |
---|---|
country | String |
state | String |
city | String |
- Save your vocabulary file.
Step 2: Creating the Form Stagesβ
The form will guide the user through three selection stages. All data will come from a single, public API endpoint.
Stage 0: Select Countryβ
- Create a New > Rulesheet named
Stage0_Country.ers
. - Set the Precondition to
UI.currentStageNumber = 0
. - In the Actions section, add the following rules:
- Set Data Path:
UI.pathToData
='Location'
- Create Container:
UI.containers
=Container.new
with atitle
of'Select Your Location'
. - Create Country Dropdown: Add a
UIControl.new
.type
='MultipleChoices'
fieldName
='country'
label
='Country'
dataSource
='https://api.npoint.io/43503523177704555543'
- Configure DataSource Mapping: Add a
DataSourceOptions.new
to the dropdown control.dataValueField
='country'
dataTextField
='country'
- Set Next Stage:
UI.nextStageNumber
=1
- Set Data Path:
Stage 1: Select Stateβ
The options in this dropdown will be filtered based on the country chosen in Stage 0.
- Create a New > Rulesheet named
Stage1_State.ers
. - Set the Precondition to
UI.currentStageNumber = 1
. - Add the following rules:
- Create Container:
UI.containers
=Container.new
with atitle
of'Select Your Location'
. - Create State Dropdown: Add a
UIControl.new
.type
='MultipleChoices'
fieldName
='state'
label
='State'
dataSource
='https://api.npoint.io/43503523177704555543'
- Configure State Filtering: This is the key step. Add a
DataSourceOptions.new
to the dropdown.dataValueField
='name'
dataTextField
='name'
pathToOptionsArray
=$.countries[?(@.country == '
+ Location.country +')].states[*]
- This JSONPath finds the country object that matches the user's selection (
Location.country
) and then drills down to get the list of itsstates
.
- Set Next Stage:
UI.nextStageNumber
=2
- Create Container:
Stage 2: Select Cityβ
This final dropdown is filtered by both the previously selected country and state.
- Create a New > Rulesheet named
Stage2_City.ers
. - Set the Precondition to
UI.currentStageNumber = 2
. - Add the following rules:
- Create Container:
UI.containers
=Container.new
with atitle
of'Select Your Location'
. - Create City Dropdown: Add a
UIControl.new
.type
='MultipleChoices'
fieldName
='city'
label
='City'
dataSource
='https://api.npoint.io/43503523177704555543'
- Configure City Filtering: Add a
DataSourceOptions.new
to the dropdown.dataValueField
='name'
dataTextField
='name'
pathToOptionsArray
=$.countries[?(@.country == '
+ Location.country +')].states[?(@.name == '
+ Location.state +')].cities[*]
- This path is even more specific, finding the correct country, then the correct state, and finally retrieving the list of its
cities
.
- End the Form:
UI.done
=true
.
- Create Container:
Step 3: Assembling the Ruleflowβ
- Create a New > Ruleflow named
FilterByCountryStateCity.erf
. - Drag all three of your rulesheets onto the canvas.
- Do not connect them. The rules will handle the stage progression.
Step 4: Testing Your Logicβ
Testing this ruleflow verifies that the dynamic JSONPath expressions are constructed correctly.
- Test Case 1 (State Filtering):
- Input:
UI
object withcurrentStageNumber = 1
. ALocation
object withcountry = 'USA'
. - Expected Output: The
UI
object should havenextStageNumber = 2
. ThepathToOptionsArray
for the state dropdown should be set to$.countries[?(@.country == 'USA')].states[*]
.
- Input:
- Test Case 2 (City Filtering):
- Input:
UI
object withcurrentStageNumber = 2
. ALocation
object withcountry = 'Canada'
andstate = 'Ontario'
. - Expected Output: The
UI
object should haveUI.done = true
. ThepathToOptionsArray
for the city dropdown should be set to$.countries[?(@.country == 'Canada')].states[?(@.name == 'Ontario')].cities[*]
.
- Input:
Conclusionβ
Excellent work! You have successfully built a three-level cascading selection form, which is a common and powerful pattern in web applications. You have mastered how to use data from previous stages to dynamically filter options for the user, creating a clean and intuitive experience. This concludes our hands-on tutorials.