Adding logic to your forms can help make them "smart" by improving the user experience, promoting cleaner data, and automating calculations.
Form logic includes:
- calculations
- cross-form logic
- skip logic
- constraints
When writing expressions for form logic it is necessary to use the correct syntax, including punctuation and spaces. Form logic is not case-sensitive.
Referencing Items
There are two ways to reference an item on your form within a logic expression. Values can either be an item reference (e.g. age) or the literal value of an item (e.g. yes or 1).
| Syntax | Description | Example |
|---|---|---|
| . | References the current item's value. | The expression .>= and .<=65 in an age field's constraint settings would verify that the value (age) entered is between 18 and 65. |
| ${itemname} | References the value of another item where itemname is the name of that item. | The expression ${sex} = 'female' could be used in a skip logic setting on a pregnancy question to display the question only if the participant is female. |
To use values from other forms see below.
Conditions
| Description | Syntax |
|---|---|
| Numeric Value | ${itemname} = value |
| String Value | ${itemname} = 'value' ${itemname} = "value" |
| Null Value [Empty String] | ${itemname} = '' |
| Multiple Select Numeric Value(s) | selected(${itemname}, choicename) |
| Multiple Select Character Value(s) | selected(${itemname}, 'choicename') |
Operators & Boolean Logic
| Description | Syntax |
|---|---|
| Addition | + |
| Subtraction | - |
| Multiplication | * |
| Division | div |
| Modulus (remainder) | mod |
| Equal | = |
| Not Equal | != |
| Less Than | < |
| Less Than or Equal to | <= |
| Greater Than | > |
| Greater Than or Equal to | >= |
| Manipulating the order of operations | ( ) |
| All must be true | and |
| Any can be true | or |
Boolean operators (and & or) return a value of either True or False. If you have a sequence of statements, all linked together with and, then to have a value of True every statement must be true; if even one is not true, then the whole expression's value is False. With or, the threshold is much lower: only one of the statements needs to be true to evaluate to True (though any number can be true, as long as at least one is).
Parentheses control the order of operations of a statement and are often used even when not needed to clarify and to increase the readability of the code (e.g.(${enddate}-${startdate}) * 2). Lastly, modulus (mod) divides one number into another and returns the remainder. This has many uses well beyond the EDC realm, but one of the simplest examples is that many programs use it to tell when a number is even or odd (if mod 2 is 0 it's even, 1 it's odd), or to know if a number is equal to, or a multiple of, another number (in this case if mod-ing by say 7, any value that results in 0 would be a multiple of 7).
Calculations
Enter the formula from the syntax column in the Calculation panel that appears on the left after you select Calculate in the Item Type field (Form Designer) or in the Calculation column (Form Template). Replace the items in brackets with the names of your own items.
By default, calculation items recalculate whenever any data in the form changes. To recalculate only when a specific item changes, select that item from the Triggered by dropdown. Restricting recalculation to a specific item can improve performance for complex forms.
If No specific trigger (always recalculate) is selected, the calculation behaves as a standard calculation and recalculates whenever form data changes. If a specific item is selected, the calculation recalculates only when that item changes.
| Calculates | Key | Syntax |
|---|---|---|
| BMI (adjusting for Metric or Imperial, and rounding to 2 decimal digits) | weight = weight in lbs or kgs height = height in cm or in wh_units = weight and height units (1 for cm and kgs, 2 for in and lbs) | round(if(${wh_units} = 2 and ${weight} != '' and ${height} != '', 703 * ${weight} div(pow(${height}, 2)), if(${wh_units} = 1 and ${weight} != '' and ${height} != '', 10000 * ${weight} div(pow(${height}, 2)), 0)), 2) Alternatively: round(if(${weight} != '' and ${height} != '', if(${wh_units} = 1, 10000 * ${weight} div(pow(${height}, 2)), 703 * ${weight} div(pow(${height}, 2))), 0)) |
| Mean Arterial Blood Pressure (MAP) | systolic = systolic blood pressure diastolic = systolic blood pressure | (systolic + (2 * diastolic)) div 3 |
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article