You're right to think about conditional requirements! KoboToolbox handles this elegantly with the required attribute combined with relevant logic. Here's how to make the newly rendered input field required only when the checkbox is selected:
1. Modify the Input Field's Attributes
In your form's XML (or within the KoboToolbox Form Builder UI), add both relevant and required attributes to the input field. The required attribute will be conditional, based on the checkbox's value.
<text name="additional_details" label="Additional Details" relevant="${trigger_checkbox} = 'yes'" required="${trigger_checkbox} = 'yes'"/>
Explanation:
relevant="${trigger_checkbox} = 'yes'": This ensures the field is only rendered when the checkbox is set to "yes".
required="${trigger_checkbox} = 'yes'": This makes the field required only when it's actually visible (i.e., when the checkbox is "yes"). If the checkbox is "no", the field isn't rendered, and therefore isn't considered for validation.
2. Using the KoboToolbox Form Builder UI
If you're using the UI:
- Click on the Text question (the input field).
- In the Relevant field, enter:
${trigger_checkbox} = 'yes'
- In the Required field, enter:
${trigger_checkbox} = 'yes'
How it Works
KoboToolbox evaluates the relevant and required conditions before rendering the form and before checking for required fields.
- If
trigger_checkbox is "no", the additional_details field is never rendered. Because it's not rendered, Kobo doesn't even consider it for the required field check.
- If
trigger_checkbox is "yes", the additional_details field is rendered, and the required condition becomes true, making it a mandatory field.
Complete Example XML
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jr="http://openrosa.org/javarosa">
<h:head>
<h:title>Checkbox Triggered Required Input Field</h:title>
<model>
<instance>
<data id="checkbox-triggered-required-input">
<trigger_checkbox/>
<additional_details/>
</data>
</instance>
</model>
</h:head>
<h:body>
<select1 name="trigger_checkbox" label="Do you want to provide additional details?">
<item name="yes" label="Yes"/>
<item name="no" label="No"/>
</select1>
<text name="additional_details" label="Additional Details" relevant="${trigger_checkbox} = 'yes'" required="${trigger_checkbox} = 'yes'"/>
</h:body>
</h:html>
Important Considerations:
- Question Names: Make sure you replace
trigger_checkbox and additional_details with the actual names you've given to your questions in the form.
- Testing: Thoroughly test your form with both checkbox options ("yes" and "no") to confirm that the input field appears and behaves as expected, and that the required validation works correctly.
This approach ensures that you only enforce the requirement for the input field when it's actually presented to the user, creating a more logical and user-friendly form.