How do you model form state beyond controlled vs uncontrolled?
Dirty tracking, validation timing, server errors, progressive enhancement, and the state that actually matters in production forms.
Useful prerequisites
Forms collect user input, but the state around that input grows quickly: touched fields, validation messages, submission status, server errors and unsaved changes. If those values are scattered across unrelated components, small interactions become hard to coordinate. A form architecture gives each piece of state a clear owner and lifecycle.
Forms collect user input, but the state around that input grows quickly: touched fields, validation messages, submission status, server errors and unsaved changes. If those values are scattered across unrelated components, small interactions become hard to coordinate. A form architecture gives each piece of state a clear owner and lifecycle.
The quick version
Controlled vs uncontrolled only answers “who owns the input value.” Real forms need more: dirty state, touched state, validation timing, client vs server errors, submission lifecycle, and often progressive enhancement. Treat the form as a small state machine with clear transitions instead of a bag of input values and a boolean isSubmitting.
Build the mental model
Controlled components give you a single source of truth in JavaScript. Uncontrolled components let the DOM keep the values until you read them. Both are valid. Neither tells you:
- Whether the user has changed anything since the form was loaded or reset.
- Whether a field has been visited (so you can delay showing errors).
- How to merge server-side validation errors with client-side ones.
- What the UI should show while a submission is in flight, after a success, or after a recoverable failure.
- How the form behaves when JavaScript is slow or unavailable.
Most production form bugs come from one of those gaps, not from choosing controlled over uncontrolled.
The state that actually matters
A practical model usually includes:
Values — the current field values (controlled or read from the DOM on submit).
Initial values — so you can compute dirty state and support reset.
Dirty / pristine — has anything changed? Useful for “leave page?” warnings and for disabling a Save button until there is something to save.
Touched / visited — has the user interacted with this field? Lets you avoid showing errors the moment a form mounts.
Validation errors — client-side, keyed by field. Keep them separate from server errors.
Server errors — field-level and form-level messages that came back from the last submission attempt. These often need different clearing rules (clear on next change, clear on next submit, etc.).
Submission state — idle → submitting → succeeded / failed. Sometimes you also want a “submitted” flag so you can show success UI or redirect.
Disabled / read-only reasons — not just a boolean. A form can be disabled because it is submitting, because the user lacks permission, or because a dependency has not loaded yet.
You do not need every one of these on day one. You do need to know which ones your form actually requires and keep them explicit.
Validation timing
Three common policies:
- On submit only — simplest. Errors appear after the user tries to submit. Good for short forms.
- On blur — validate a field when the user leaves it. Reduces noise while typing.
- On change (after first blur or after first submit) — gives faster feedback once the user has already engaged with the field or the form.
Mixing them carelessly produces forms that yell at the user while they are still typing. Pick a policy and apply it consistently. Client validation should be a fast, synchronous (or at least non-blocking) check; expensive or server-dependent checks belong in the submission path or in debounced async validators that still respect race rules.
Server errors and the submission lifecycle
A solid submission flow looks roughly like:
- Run client validation. If it fails, mark fields touched, show errors, stop.
- Enter submitting state. Disable the form or the submit control.
- Send the request (with abort support if the user can navigate away or cancel).
- On success: clear server errors, update initial values (so the form becomes pristine again), set success state, maybe redirect.
- On failure: map the response into field and form errors, leave the user’s values intact, re-enable the form.
Never clear the user’s input just because the server rejected it. And never leave the form in a permanent submitting state if the request fails or is aborted.
Progressive enhancement
If the form can work without JavaScript, start from a normal HTML form with a real action and method. Layer the enhanced behaviour on top:
- The same endpoint can return HTML for the no-JS case and JSON for the enhanced case.
- Client-side validation becomes an optimisation, not the only line of defence.
- Success and error handling need both a full-page path and an in-place path.
This is more work up front. It is also one of the most effective ways to make forms resilient on slow networks and flaky devices.
Concrete implementation sketch
type FormState = {
values: Record<string, string>;
initialValues: Record<string, string>;
touched: Record<string, boolean>;
clientErrors: Record<string, string>;
serverErrors: Record<string, string>;
formError?: string;
status: 'idle' | 'submitting' | 'success' | 'error';
};
function isDirty(state: FormState) {
const keys = new Set([...Object.keys(state.values), ...Object.keys(state.initialValues)]);
return [...keys].some((key) => state.values[key] !== state.initialValues[key]);
}
On change: update values, clear the matching server error if your policy says so, optionally re-run client validation for that field if it is already touched.
On blur: mark touched, run field validation.
On submit: mark all relevant fields touched, run full client validation, then enter the submission lifecycle above.
Where it gets tricky
Reset after success.
After a successful create, you often want to clear the form. After a successful edit, you usually want to treat the new values as the new initial values so dirty state resets without clearing the fields.
Dynamic fields.
Fields that appear or disappear (conditional sections, repeatable groups) need their values, touched flags, and errors created and destroyed with them. Leaving stale errors for a removed field is a common bug.
Async default values.
If initial values arrive after the form has mounted (edit form loading from an API), decide whether the form is disabled until they load and whether any user input made before load should be discarded or merged.
Multiple submit buttons.
“Save draft” vs “Publish” may send different payloads or hit different endpoints. The submission state still needs a single coherent story so the UI does not enable one button while the other is in flight.
Password and sensitive fields.
Some teams avoid controlled components on purpose for password fields so the value spends less time in JavaScript memory. Uncontrolled + read-on-submit is a valid choice here.
A common misunderstandings
-
“Controlled is always better.”
It is better when you need derived UI, cross-field validation, or tight control. It is unnecessary overhead for a simple newsletter signup that only needs values on submit. -
“If I use a form library I can stop thinking about state.”
Libraries help, but they still force you to choose validation timing, error-merging rules, and submission behaviour. The model is still your responsibility. -
“Client validation replaces server validation.”
It never does. Client checks are for fast feedback and basic invariants. The server remains the source of truth for business rules and security.
What this changes in an application
Forms are where users give you money, credentials, and content. Janky validation, lost input after a server error, or a submit button that stays disabled forever all turn into support tickets and abandoned flows. A clear state model makes the behaviour predictable for users and debuggable for the team.
Questions worth exploring next
- How would you implement “leave page?” warnings only when the form is dirty?
- How do you merge server field errors with existing client errors without flickering?
- When would you choose uncontrolled inputs even inside a mostly controlled form?
- How does progressive enhancement change your testing strategy?
References and next steps
- HTML forms — the platform baseline that progressive enhancement builds on.
- WAI-ARIA Authoring Practices — Forms — accessible naming, error association, and instructions.
- Practical form libraries (React Hook Form, Conform, Formik, etc.) — useful once you already know the state you need; read their docs for the trade-offs they chose, not as a substitute for the model.