IF {field_1} = “value x”, THEN {field_2} “IsRequired”
The above is a representation of a conditional validation statement. In simple terms, this feature is used whenever you have a form that has some fields in it that only need to be filled out if another field meets a certain condition. For example, maybe you have a checkbox field, and if its checked, then the user is required to fill out some other areas of the form. However, if its unchecked, the user can skip those other areas.
You can do this with any field type, not just checkboxes. For example, you could create a validation where if a text field (or numeric field) has a value of X, then require the user to fill out the others fields, otherwise don’t require those fields.
Here’s an example of what that looks like in the mobile app. In the example below, if you check that checkbox on the left side of the screen (were any issues observed at any of the properties in the neighborhood), then the user will not be able to submit the form until they select a value from the “properties with issues” multiselect listbox or fills out the table below it. And if the user leaves that checkbox blank, then the user will not have to fill out the other fields mentioned.
Syntax and attributes of this feature
The syntax of this expression is as follows:
“requiredWhen”: “field_id~operator~value“
The “requiredWhen” setting is a 2 or 3-part statement in the following format:
“field_id~operator” or “field_id~operator~value”
field_id: ID of the field to compare with
value: the value to compare the field (above) with. This could be a single number or string, or a list of values (optional)
Operators:
You can compare against a specific value or range of values using the following operators:
- checked : checks if the checkbox with specified ID is checked.
- unchecked : checks if the checkbox with specified ID is unchecked.
- = : checks if the value of the field is equal to one of the items in the value list. Example: cityField~=~’Boston’,’Chicago’
- <> : checks if the value is not equal to the value in the field
- < : for numeric fields…checks to see if the value is less than the value in the field
- > : for numeric fields…checks to see if the value is greater than the value in the field
- >= : for numeric fields…checks to see if the value is less than or equal to the value in the field
- <= : for numeric fields…checks to see if the value is greater than or equal to the value in the field
Here’s an example:
“requiredWhen”: “issues_yn~checked”
In the example above, there is a field (issues_yn) . The validation control is checking to see if that checkbox field is checked.
All of this may seem a bit complicated, and yea, its more complicated-looking that we’d like. A future release will make this easier to use. For now, suffice it to say that if you have some conditional validations you need on your form, it can be done.
Want to give XForms a try?
Click/tap the button below.