Advanced form logic
This page provides guidance for applying advanced form logic
Last updated
This page provides guidance for applying advanced form logic
Last updated
The logic feature is extremely powerful and there are almost limitless possibilities to what expressions can be written and applied to your form. This guidance note introduces some of the main logic options available and gives practical examples of how they can be applied.
The following comparison operators are available
Equal
==
Not equal
!=
Greater than
>
Greater than or equal
>=
Less than
<
Less than or equal
<=
Element in array or string
in
This is an example of the !( > ) symbol being used to show or hide pages based on a respondent's age.
In this example the the page 'Food Preferences' is hidden if a respondent is aged 15 or less.
The expression is applied to the 'Food Preferences' page and asks whether the respondent's age is not greater than 15.
Only if the answer is 16 or above will the expression = false and the page will be displayed.
The following expression can then be applied to the 'Not Eligible' page so this page appears if the respondent's age is 15 or less.
The result for a survey respondent is as follows:
Add, Concat
+
Subtract
-
Multiply
*
Divide
/
Divide and floor
//
Modulus
%
Power of
^
Logical AND
&&
Logical OR
||
In this example the || symbol is used to display or hide a question based on the answers to two preceeding questions.
The above expression works as follows:
It is true and therefore hidden when ('do you have children that go to school is yes) OR (do you have children that go to university is yes)
The expression is then inverted using ! to display it for the same configuration.
The in operator can be used to check whether an element appears in a list of options
In this example the in operator is used to display or hide a question based on a selection from a proceeding checkbox list.
The expression is true and therefore hidden when 'apples' is selected as a response to the question 'what fruit do your like to eat?'
The expression is then inverted using ! to display it for the same configuration.
The ternary operator, also known as the conditional operator, is very useful when writing complex logical expressions. It works in a similar way to the 'if' function in excel.
It works by first checking to see if the first segment of an expression is true. If so the consequent segment is evaluated. Otherwise, the alternative is. If the consequent section is missing, the test result itself will be used instead.
In this example the ternary operator is used to hide or display the question 'Did you like this survey' based on the respondent's answer to the question 'Do you agree to participate in this survey'
The above expression works as follow:
The first statement is evaluated initially. It asks whether the answer to the question 'do you agree to participate in this survey' is 'Yes'.
If the first statement equates to false the alternative is returned. The alternative appears at the end of the logical expression after the : symbol. In this instance the alternative returns true**.**
If the first statement equates to true the consequent segment is evaluated. The consequent statement appears immediately after the ? symbol. In this instance the consequent returns false.
In this example the ternary operator is used to display or hide a page based on the answers to three preceeding questions.
The above expression works as follow:
The first statement is evaluated initially. It asks whether the option 'a person without disabilities' has been selected as an option from the question 'Which do you consider yourself to be?
If it equates to false the alternative is returned. The alternative appears after the : at the end of the statement and in this instance is true**.**
If it equates to true the consequent segment is evaluated.
In this instance the consequent segment asks whether the answer to the question 'do you currently work for the government of the country you live in' is NOT equal to No or whether the answer to the question 'do you currently work for a United Nations Country Team is NOT equal to No. This expression will only be false if the answer to both these questions is 'No'.
The result of this logical expression is return a true expression result (and hide the page) in all circustances except if the respondent is a person without disabilities who does not currently work for the government of the country they live in or currently work for a United Nations Country Team.
Parentheses operate as you would expect them to. They are used in logical expressions primarily to modify the normal order of operations. In an expression involving parentheses, the terms inside the parentheses () are evaluated first.
They can be combined with the ! expression to invert the expression within the brackets as shown in a number of examples given above.
The application adds custom transforms to simplify expressions. Transforms use the | pipe operator.
You can use the 'length' operator to customise the number of checkbox optionsd you require a respondent to select.
For instance limiting the number of answers of a question `What colors do you like` to no more than 2 answers, add this expression to the validity logic expression:
A custom validation message can then be added under the settings option. This message will appear if a respondents checks 3 or more colours. The message will be announced to screen readers.
If a respondents selects more than 3 colours they will be able to continue to fill in the survey. However they will not be able to submit the survey until the required number of options have been selected. Respondents will be redirected to the 'invalid' question so the issue can be resolved.
This short video shows how the validity logic can be applied to customising the number of boxes of a checkbox question you require a respondent to select.
In a similar way, the 'length' operator can be used to customise the number of order question items to be ordered by a respondent.
For instance to require respondents to order all the items in this list the following validity expression can be used:
The expression result will be 'true' (and therefore 'valid') when:
The number of options ordered is more than 2 (ie all the options are selected)
OR (||) no options are selected. The number of options selected is blank ("")
A custom validation message can be added under the settings option. This message will appear if a respondents orders some but not all of the options. The message will be announced to screen readers.
If a respondents orders some but not all of the options, they will be able to continue to fill in the survey. However they will not be able to submit the survey until either all or none of the options have been ordered. Respondents will be redirected to the 'invalid' question so the issue can be resolved.
This short video shows how the validity logic can be applied to customising the number of order question items to be ordered.