Why we moved validation to the server
Inline validation felt faster until it started disagreeing with what the server enforced. What we kept on the client, what we moved, and the one rule we use now.
Inline validation felt faster to build and, for a while, faster to use. Type an email, get an answer within a few hundred milliseconds, move on. The problem showed up once our server-side rules drifted away from what the client checked.
Two answers from one form
A client-side pattern accepted an address our server later rejected for a reason the client could never have known: an account already existed for it. The inline check said "looks good", and the submit step said otherwise, three seconds later.
That gap between two different answers, from the same form, is worse than either answer alone. People trust the first thing a form tells them. When the second answer contradicts it, they do not conclude that the server knows more; they conclude that the form is broken.
People trust the first thing a form tells them. Make sure it is something the form actually knows.
What stayed on the client
We kept inline checks for anything the browser can answer completely on its own, with no knowledge of the world beyond the field:
- Is this shaped like an email address?
- Is the password at least twelve characters?
- Is the date in the future, and is the end after the start?
Those checks cannot be contradicted later, because nothing the server knows could change their answer. They run on blur, not on every keystroke, so nobody is told off for an address they have not finished typing.
In the browser
- Shape of an email
- Length of a password
- Order of two dates
Runs on blur
On the server
- Account already exists
- Invite code still valid
- Slot not already booked
Runs on submit
What moved to the server
Every rule that depends on stored state moved to submit time: duplicate accounts, expired invite codes, a booking slot someone else took a moment ago. The server now answers in the same voice as the client, with a message that names the fix, attached to the field it is about:
{
"field": "email",
"code": "account_exists",
"message": "There is already an account for this email. Sign in instead, or reset the password."
}
The form renders that message exactly where an inline error would have appeared, with the same styling and the same aria-describedby link. From the reader's side, there is one kind of error, and it is always right.
The rule we use now
It fits on a sticky note, which is the test we hold every rule to:
- Check in the browser only what the browser can know for certain.
- Check everything else once, on submit, on the server.
- Show both kinds of error in the same place, in the same words.
Beyond forms
The lesson generalizes past forms. Any time two parts of a system answer the same question independently, they will eventually disagree, and the disagreement will surface at the worst possible moment for whoever is using it. Pick one place to own each answer, and let everything else ask it.