I’m wondering whether it’s possible to restrict the input of the user in Q&A’s. For instance, I don’t want the user to be able to insert “0 days” in a duration datafield.
Validation of answers seems easy, but quickly gets quite complex once you start thinking about it. For example, assuming the user enters “0 days”:
- should the zero simply be cleared?
- or should it be set to at least 1?
- should the user receive an error message, or should the correction happen silently?
- should the error message be the same every time, or tailored to the input given?
- is the 0 days always incorrect? There could be situations, or combinations of inputs, where the 0 days is actually acceptable as input, so maybe some exceptions should be allowed.
Due to the variety of circumstances, and the fact that only some users seem to ask for validation, we do not (yet) offer an “out of the box” solution. The following possibilities exist, however:
1. Inserting a “comment” or “warning” question below the duration-question, that will only show up when the input to the duration-question is invalid (e.g., 0 days).
2. Anticipating on invalid or missing functions within the clause itself, e.g., by inserting an error message with
!! ... !!
3. Fairly advanced, but you can insert a custom function. The following custom function will for example alert the user, and wipe out the invalid answer (don’t forget to set an identifier “my-duration” for your question):
(assoc question :action-fn (fn [q duration] (when-not (pos? (:amount duration)) (do (show-error "The duration must be greater than zero!") (set-answer "my-duration" nil)))))
I was familiar with the first option, but not with the second and third, thanks a lot.
By the way, option 1 and 2 are my personal preferences. See the red box in the discussion on text validation on why you should be aware that too much validation can annoy users.
Thank you Maarten. That’s a very useful link for future reference!