This blog post is all about using the relevant column in your XLSForm to help you control the visibility of questions in your survey. Mastering this is an important smart form design skill. A good smart form will never show end users a question unless it is necessary. The days where an endless collection of questions and data entry boxes are presented to the user should be over. The relevant column is present in the XLSForm survey worksheet and can optionally hold an expression. If the expression evaluates to true, the corresponding question will be shown. If the expression evaluates to false, it will be hidden. You can use data anywhere from your form as part of your expression, making your forms dynamically change based on the user's responses.
Lets go first through some examples:
Say you only want to ask a question if the response to a previous question is 'yes':
type | name | label | relevant |
---|---|---|---|
select_one yes_no | owner_present | Is the house owner present? | |
text | owner_name | Name: | selected(${owner_name},'yes') |
Other relevant expressions may include:
The expression syntax uses the XLSForm specification. To learn more about how to write your own expressions check the Formulas—Survey123 for ArcGIS | Documentation help topic.
When using mathematical operators, one needs to be careful with empty responses. If a question has not been answered, it evaluates to 0. This would make for example ${PreviousQuestion} < 18 always return true, if ${PreviousQuestion} has not been answered. To resolve this issue, you can add your expression to an if block as follows:
The safest way to check if a question has been answered is using string-length(${QuestionName}). If string-length returns 0, then you known the answer is empty. This function works well with question types of all sorts: with numbers, text, images, dates etc
In some cases, you may want to use regular expressions (regex) For example, the following expression can be used with a note type of question. If the expression evaluates to true, the note is shown, otherwise it is hidden.
type | name | label | relevant |
---|---|---|---|
text | Your e-mail | ||
note | Type a correct e-mail | regex(${Q2}, '[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}') |
A special case when using relevant statements are select_multiple questions. For them, the count-selected and selected statements are very handy. In the example below, the 'Select other topping:' question only appears if the choice 'Other' has been selected from the list of toppings. The count-selected() function is used to display a 'Too many toppings' warning if more than 2 toppings have been selected.
type | name | label | relevant |
---|---|---|---|
select_multiple toppings | toppings | Pizza toppings | |
text | top_other | Other topping: | selected(${toppings},'Other') |
note | Too many toppings! | count-selected(${toppings})>2 |
To learn more about select_multiple questions, have a look https://community.esri.com/groups/survey123/blog/2017/08/30/understanding-multiple-choice-questions-...
Relevant expressions can be applied to many different types of questions: select_one, integer, text, image, barcode, repeat etc. You can also apply them to notes as well as to Groups and Repeats. Groups are of particular importance because a relevant statement applied to a Group will have effect over all questions within that Group. If you have three or four questions to which you want to apply exactly the same relevant expression, you may want to consider creating a Group and apply the expression only to the Group. This will make your Survey load faster because you will be reducing the number of times the expression needs to be evaluated.
Another aspect to keep in mind is the effect relevant statements have on submit. If a question is not relevant (it is hidden), then data in that question will not be submitted to ArcGIS.
To learn more, check out the video tutorial below!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.