In SurveyCTO forms the first thing you specify about a field is the type of field it is, which limits what type of data it can hold. This is the most basic way of limiting what can be entered into a field. Beyond the data type, you can include more specific limits on what can be entered into a field, which we call "constraints" in SurveyCTO. A constraint is a condition (or expression) that you can set that must be satisfied before a value entered into a field is accepted, and in this way, you can validate responses.
It is important to understand how constraints interact with another more basic field property: whether a field requires an answer. For more on the required field parameter, please read about other field properties. A constraint evaluates the value which is captured in a field. It is possible to pass a field which is not required without recording an answer, which would defeat a constraint as well. So a field being required is an important companion to a constraint.
So how can you add a constraint to a field? SurveyCTO has features to help you create constraint expressions. While you're designing a form in the online form designer, when you configure a field, there's the constraint wizard. Or on the Design tab of the server console, under Tools, there's the option to build a constraint.
Let's say you want to build a constraint that limits the range of responses for an integer field in to the possible range of values for legal adults (e.g. 18 to 100). You might also want to include an allowable exception, outside this range, to signify "don't know" (allowable exceptions are discussed further in part 2 of this guide). You can also build such a constraint using the Design tab's constraint builder:
Building a constraint
The steps you can see in the video above are the following:
- Under Your forms and datasets, click on Tools.
- Click on the Build constraint option.
- Choose the Numeric constraint type.
- Enter values in the available constraint parameters:
- Minimum value (18 in this example).
- Maximum value (100 in this example).
- Allowable exceptions (-99 in this example).
- A message to display when the constraint is violated (also known as a "constraint message").
- Click on Next.
- Select and copy the constraint expression and constraint message into a form design.
This last step applies only to the Design tab's tools. In the online form designer's wizard (which works the same otherwise), the constraint expression and constraint message will be put in the correct place. If you're working in a spreadsheet template, find the constraint and constraint message columns on the survey sheet and copy the constraint expression and constraint message over from the constraint builder into the row in the spreadsheet for the right field.
The resulting constraint expression is as follows:
(.>=18 and .<100) or .=-99
The above is also just one general example of a common type of constraint (numeric). Much more is possible and precisely how to constrain responses will vary depending on field type. Read further in part 2 of this guide for an explanation of the period (".") used in the expression.
Testing a constraint
In addition to the tools that can help to build constraints, there's also a constraint tester available on the Design tab, under Tools. This can be very helpful, to test constraints, to confirm that they work correctly.
Following the above video, select Tools on the Design tab under Your forms and datasets and and click on Test constraint. Type or paste your constraint expression into the Constraint field, then click Next. Then on the second screen, enter test values for the fields named in the constraint expression and/or a value into Entry to test. Click on Test constraint and your entered values will return Failed when the test value/s violate the constraint (preventing progress in the form), or Succeeded in the case that the constraint evaluates as true (allowing progress).
This article is the first part of this feature guide. Click here to read part 2 on additional properties and considerations for constraint expressions. You can also jump to the last part by clicking here, to see working example constraints.