Conditional Logic in Forms: Questions Only the Right Person Sees
Instead of a 40-question form everyone has to wade through, a form that shows each person only the questions that apply to them.
Say you have a job application form. If the applicant says "I have no work experience," the next ten questions about their previous employer are meaningless. Conditional logic solves exactly this: questions are shown or hidden based on earlier answers.
Three things logic can do
- Show/hide: the simplest and most common case.
- Jump between pages: "If you're not a customer, go to the final page."
- Screen out of the survey: when someone doesn't meet a required condition, letting them continue only produces irrelevant data.
Trap one: the answer to a hidden question
The user saw a question, answered it, then changed the answer to an earlier question, and now that question is hidden. What should happen to the answer?
If you keep it, your report contains an answer the user effectively withdrew. If you delete it and the user comes back, they have to fill it in again. The correct behavior: the answer to a hidden field is dropped at final submission. Keep it while the user is in the form (so it isn't lost if they come back), but leave it out of the final data.
Trap two: loops
"If A is filled in, show B" and "If B is filled in, hide A." These two rules trigger each other endlessly. Any logic engine has to detect loops and stop them; otherwise the form freezes in the user's browser.
How to stay away from loops
One simple rule solves 99% of cases: only write rules that point forward. Question 5 can depend on the answer to question 3, but question 3 must not depend on the answer to question 5.
Trap three: server-side validation
Logic runs in the browser so the user sees the result immediately. But the browser can be tampered with. If a field is "required only when A = yes," the server has to know that too; otherwise someone can submit the form without that field.
In other words, the logic has to run twice: once in the browser for the experience, and once on the server as the source of truth.
When you're overdoing the logic
If your form has more than 15 rules and you yourself no longer know what is connected to what, you should probably build two or three simple forms instead of one complex one, and route the user to the right form from the first page.