If-Then-Else Constraints

If-Then-Else logic can be used to create conditional logic in calculated fields. In this example, we will be setting a calculated input box to display different values based on the selected option of a checkbox widget. If-Then-Else logic can be used in both calculated fields and aggregate fields.

  1. Add a checkbox widget to the form and enter a label for it.
  2. Add an Input box widget to the form and enter a label for the widget.
  3. In the Properties section click on the Calculated radio button, this will bring up the Expression Builder.
  4. From the Expression Builder click on Operations and under If/Then/Else Clause select If() Then() Else() to add the expression to the value box.

    In an If-Then-Else expression, the If parentheses will contain a condition, the then parentheses will contain the expression which will be evaluated if the If condition is met, and the else parentheses will contain the expression which will be evaluated if the condition is not met.

    You can nest If-Then-Else conditions together if there are more than two possible values for the field by placing another If-Then-Else inside the Else parentheses of the first If-Then-Else expression.
  5. In this example the If condition will be based off of our checkbox field. With the cursor icon inside the If parentheses, click Field.
  6. From the Select a Field window, be sure FORM is selected for the Collection then click on the checkbox data field.
  7. The checkbox field will now appear inside the If parentheses, to finish the expression for the calculated field's condition type ="true" so that the condition reads If(checkbox="true")then()else(). Be sure true is lower case and in quotes.
  8. Place the cursor inside the Then parentheses, this is where we will define the expression that will be used when the If condition is met. For this example let's just enter the text "yes". Be sure to include the quotation marks.
  9. Now place the cursor inside the Else parentheses. We will now define the expression that will be used if the condition is not met. For this example let's just enter the text "no". Be sure to include the quotation marks.
  10. Our If-Then-Else logic is now set up to display 'yes' if the checkbox is checked and 'no' if the checkbox is not checked. Click Save and Exit to save the changes to the form and exit the Form Editor.