Sub topic 6
Conditional Fields and Dependent Logic
Assume you know basic HTML form controls, JavaScript or TypeScript, and how a browser submits form data. This chapter focuses on a common source of bugs: fields whose visibility, validity, or meaning depends on another field.
A conditional field appears only when an earlier answer makes it relevant. For example, an address is needed when a customer chooses shipping, but not when they choose pickup. The dependency has three parts:
- Controller: the field that determines the branch, such as
delivery. - Dependent field: the field affected by that choice, such as
address. - Rule: the condition that decides whether the dependent field is visible and required.
Define the dependency explicitly
Do not scatter conditions across rendering, validation, and submission code. Define the field rules in one place. The same rule should control all three behaviors.
type Values = Record<string, string>;
type Field = {
name: string;
visibleWhen?: (values: Values) => boolean;
};
const fields: Field[] = [
{ name: "delivery" },
{ name: "address", visibleWhen: (v) => v.delivery === "ship" },
];
function activeValues(values: Values): Values {
const result: Values = {};
for (const field of fields) {
if (!field.visibleWhen || field.visibleWhen(values)) {
if (field.name in values) result[field.name] = values[field.name];
}
}
return result;
}activeValues removes values for fields that are not currently relevant. This prevents an old address from being submitted after the user changes from shipping to pickup.
Clear or preserve hidden values
When a controller changes, decide what happens to the dependent value.
| Policy | Use when | Risk |
|---|---|---|
| Clear immediately | The old value could cause an incorrect order or privacy issue | The user must re-enter it if they return |
| Preserve temporarily | Users commonly switch branches while comparing options | Stale data can be submitted accidentally |
| Preserve in a draft only | Returning to the branch should restore work | Requires careful filtering before submission |
A safe default is to preserve values in local form state but filter inactive fields from validation and submission. If the value has security, privacy, or financial consequences, clear it when the branch changes.
Validate only active fields
A hidden field should not block the user from continuing. Validation should receive the current branch, or it should first calculate active values.
For example:
- Validate
delivery. - If the value is
ship, requireaddress. - If the value is
pickup, ignoreaddress. - Submit only the active fields.
This rule applies to server validation too. Client-side visibility is not a security boundary. The server must recalculate which fields are allowed for the selected branch and reject contradictory combinations.
Handle dependencies in order
Some forms have dependent chains. A country controls the available states, and the state controls the available cities. When the country changes:
- Clear or revalidate the state.
- Clear or revalidate the city.
- Load new options if needed.
- Move focus or announce the update when the change affects keyboard users.
Avoid rendering a required child field before its parent choice is known. Represent an unknown state separately from an empty answer, because “not selected yet” and “selected no value” often need different messages.
Make conditional changes understandable
Use a real <label> for every visible control. Do not rely on placeholder text to explain why a field appeared. If a selection reveals new controls, place them near the controller and give the group a clear heading.
For screen reader users, a newly inserted field may need an announcement through a polite live region. Do not move focus unexpectedly for every small change. Move focus only when the new section requires immediate attention, such as after selecting an option that reveals an error.
Test branch changes
Test each path, including changing an answer after entering dependent data:
- Select shipping and submit without an address.
- Enter an address, switch to pickup, and confirm the address is not submitted.
- Switch back to shipping and verify the chosen preservation policy.
- Reload a saved draft where the controller and dependent value disagree.
- Submit crafted data containing fields from an inactive branch.
Exercise
A form shows a company tax ID when customerType is "business". The user enters a tax ID, changes the type to "individual", and submits. What must the client and server do?
Show answer
Both must treat the tax ID as inactive and exclude it from the submitted business data. The server must independently enforce this rule rather than trusting the client.