|
| 1 | +# Adding required logic in XLSForm |
| 2 | + |
| 3 | +Required logic allows you to make a question mandatory if specific conditions are met. For example, you can require a phone number question only if respondents agree to be contacted in the future. This option provides more control than simply marking a question as always required or always optional. |
| 4 | + |
| 5 | +<p class="note"> |
| 6 | + To learn more about required questions and how to customize the message shown to respondents when they leave a required question unanswered, see <a href="https://support.kobotoolbox.org/question_options_xls.html#required-questions">Using question options in XLSForm</a>. |
| 7 | +</p> |
| 8 | + |
| 9 | +This article explains how to add required logic conditions in XLSForm, including making a question required based on whether another question was answered. |
| 10 | + |
| 11 | +<p class="note"> |
| 12 | + <strong>Note:</strong> This article focuses on adding required logic in <a href="https://support.kobotoolbox.org/getting_started_xlsform.html">XLSForm</a>. To learn about adding required logic in the KoboToolbox Formbuilder, see <a href="https://support.kobotoolbox.org/question_options.html?highlight=custom+logic#mandatory-response">Using the question options</a>. |
| 13 | + <br><br> |
| 14 | + For hands-on practice with required logic in XLSForm, see KoboToolbox Academy’s <a href="https://academy.kobotoolbox.org/courses/xlsform-fundamentals">XLSForm Fundamentals Course</a>. |
| 15 | +</p> |
| 16 | + |
| 17 | +## Adding required logic conditions |
| 18 | + |
| 19 | +Required logic uses <a href="https://support.kobotoolbox.org/form_logic_xls.html#question-referencing">question referencing</a> to make questions required based on previous answers. The question used to define the relevance logic is referred to as the **reference question.** |
| 20 | + |
| 21 | +To add required logic in XLSForm: |
| 22 | +1. Add a **required** column to the `survey` worksheet. |
| 23 | +2. In the row of the question you wish to set the required logic for, enter the condition that must be met in order **for the question to be mandatory.** |
| 24 | + |
| 25 | +**survey worksheet** |
| 26 | + |
| 27 | +| type | name | label | required | |
| 28 | +|:--------------|:-----------|:--------------------------------------------------------------------------|:------------------| |
| 29 | +| select_one yn | recontact | Do you agree to being contacted again for another study in the future? | | |
| 30 | +| text | email | What is your email address? | ${recontact} = 'yes' | |
| 31 | +| survey | |
| 32 | + |
| 33 | +If a respondent does not answer a required question, they will not be able to proceed to the next page of the form or submit it. |
| 34 | + |
| 35 | +### Formatting required logic conditions |
| 36 | + |
| 37 | +The format of the required logic condition will differ according to the **type** of the reference question, as detailed in the table below. |
| 38 | + |
| 39 | +| Reference question type | Skip logic condition | Example | |
| 40 | +|:-------------------------|:--------------------|:---------| |
| 41 | +| select_one | `${reference_question} = 'choice_name'` | `${consent} = 'yes'` | |
| 42 | +| select_multiple | `selected(${reference_question}, 'choice_name')` | `selected(${reasons}, 'other')` | |
| 43 | +| integer | `${reference_question}` followed by a logical operator (>, <, =) and a number (or a reference to another question) | `${age} >= 18` | |
| 44 | +| date | `${reference_question}` followed by a logical operator (>, <, =) and `date('YYYY-MM-DD')` | `${dob} >= date('1975-01-01')` | |
| 45 | + |
| 46 | +<p class="note"> |
| 47 | +To learn more about building form logic expressions in XLSForm, see <a href="https://support.kobotoolbox.org/form_logic_xls.html">Introduction to form logic in XLSForm</a>. |
| 48 | +</p> |
| 49 | + |
| 50 | +## Adding required logic based on whether a question was answered |
| 51 | + |
| 52 | +In addition to setting required logic for a specific response, you can also base it on whether a question was answered or left blank. This is useful when you want to ensure that at least one of two questions is required. |
| 53 | + |
| 54 | +Unanswered questions are treated as empty strings, noted as two single apostrophes (‘’). The following skip logic conditions can be used: |
| 55 | + |
| 56 | +| Relevance logic condition | Description | |
| 57 | +|:---------------------------|:-------------| |
| 58 | +| `${reference_question} != ''` | Require only if `reference_question` is answered (not blank). | |
| 59 | +| `${reference_question} = ''` | Require only if `reference_question` is unanswered (blank). | |
| 60 | + |
| 61 | +**survey worksheet** |
| 62 | + |
| 63 | +| type | name | label | required | |
| 64 | +|:------|:--------|:----------------------------------------------------|:-------------| |
| 65 | +| note | contact | Please provide your phone number or email address below. | | |
| 66 | +| text | phone | Phone number | | |
| 67 | +| text | email | Email address | ${phone} = '' | |
| 68 | +| survey | |
0 commit comments