Validation Rules
Validation Rules
The third type of form rule listed under the Form Rules tab is Validation Rules. You can create Validation Rules to ensure that certain restrictions are applied before the form is submitted.
With Validation Rules, you can run rules to validate against custom values, other form fields, and form connection fields.
Validate against custom values
You can create validation rules to validate against custom values.
For instance, if you have an Add New Job form, you can create a validation rule to restrict the form from being submitted when a user adds a job with a start date that is older than 14 days from the current date and display a custom error message of "You can not add a job that is older than 14 days."
To create this validation rule, click on the Form Rules tab > Validation Rules > Add New Validation Rule button.
Then create the validation rule to restrict the form from being submitted when a user adds a job with a start date that is before the previous 14 days and display a custom error message of "You can not add a job that is older than 14 days."
Now, when a user attempts to submit a form with a job that is older than 14 days, the form will not be submitted and the above error message will display.
The above example demonstrates running a validation rule against a custom value of "before the previous 14 days." Alternatively, you can also run validation rules to validate against other fields within the form, which we will discuss below.
Validate against form fields
You can create validation rules to validate against other fields within the form.
For instance, you can create a validation rule to restrict the form from being submitted when a user adds a job with an end date that is before the start date and displays a custom error message of "You chose an end date that is before the start date. Please revise your job dates to successfully submit this job."
Now, when a user attempts to submit a form with a job end date that is before the job start date, the form will not be submitted and the above error message will display.
Validate against form connection fields
You can also run validation rules to validate against connection fields within the form.
For instance, say you have an Events Management App with two data tables:
- Events data table
- Locations data table with a one-to-one connection field to the Events data table (each event is assigned to one location).
Now, say that you want to create a validation rule to prevent the form from being submitted when a user adds a new event and selects a location that is already booked.
To create this validation rule for the Add New Event form, select the Locations connection field and select "is not unique" to restrict this form from being submitted if the location selected is already booked/is not unique. You can also add a custom error message to display when this run is met, such as "The location you selected is unavailable. Please select another location and resubmit this event."
Now, when a user attempts to add an event with a location that is already booked (not unique), the form will not be submitted and the above error message will display. The form will only submit once the user selects a location that is not already booked (is unique).
The above example refers to validating against a connection field that has a one-to-one connection. You can also validate against a connection field that has a one-to-many connection.
When running a validation rule against a connection field that is a one-to-many connection, you can choose to validate if all or any value is unique.
For instance, say you have an Events Management App with two data tables:
- Events data table
- Performers data table with a one-to-many connection field to the Events data table (each event is assigned to one or more performers).
Now, say that you want to create a validation rule to prevent the form from being submitted when a user adds a new event and selects performers that are all already booked together for a different event.
To create this validation rule for the Add New Event form, select the Performers connection field and select "All values are not unique" to restrict this form from being submitted when all performers selected are not unique/are already booked together for a different event. You can also add a custom error message to display when this rule is met, such as "All performers selected are already booked. Please select another performer and resubmit this event."
Now, when a user attempts to submit a new event with performers who all are booked together for a different event (all are not unique), the form will not submit and the above error message will display. As you can see in the gif below, a user attempts to add a new event with the Frozen Princess and Clown performers, which are already both booked together for a different table as you can see in the Events table component. Therefore, the error message displays, and the form is not submitted.
However, if the user attempts to resubmit the event with an additional performer that is unique/not already booked, the validation rule will not apply since not all values are not unique (meaning, at least one performer selected is unique) and the form will be successfully submitted.
Alternatively, you can choose to restrict the form from being submitted if any of the values selected are not unique. For instance, even if one performer selected is unique/not already booked but the other performer(s) selected are not unique/are already booked, the form should not be submitted.
To create this validation rule that restricts the new event from being submitted if any of the performers selected are not unique, select the Performers connection field and select "any values are not unique".
Now, when a user attempts to add a new event with even just one performer selection that is already booked/is not unique, the form will not be submitted, and the above error message will be displayed.
You can also create validation rules that only run when both the first condition and the second condition are met. For instance, you can create a validation run to restrict the form from submitting if the location is blank and the performer is blank. To create this rule, simply click on the + arrow to the right of the first condition to create the second condition.
Now, when a user attempts to submit a new event and leaves the location blank and the performer blank, the form will not be submitted.
Adding field values to error messages
In addition to creating basic custom error messages for each validation rule, you can also add field values to serve as field placeholders within the custom error messages to further customize the messages.
To add field values to an error message, click on the Add Field button and select the desired field value you wish to include in the message.
For instance, as illustrated in the image above, you can create a validation rule to prevent a new job from being submitted when a user selects an end date that is before the start date and display a customized error message of "You chose an end date of (End Date) that is before the start date (Start Date)."
This error message will appear as follows:
Checking for additional fields
When creating a validation rule, you also have the option to check for additional fields by selecting the Check Additional Fields checkbox.
For instance, if you have a Feature Voting Platform where users can vote on features, you want to ensure that each user can only vote for each feature only one time. Users can vote for multiple features, but you want to ensure that they can only vote for each feature only once.
You can accomplish this by creating a validation rule to prevent the vote from being submitted if the Voter (the auto-logged-in user) is not unique and the Feature is also not unique. Check the Check Additional Fields checkbox and select the Feature field to check if there is any existing record of the auto-logged in voter voting for the selected feature.
Now, when a user attempts to submit a vote for a feature that is not unique to this voter, the form will not submit and the above error message will display. As you can see in the Votes table component below, logged-in voter Bob Lowell has already voted for the plugin feature and therefore can not vote for this feature again.
Please Note: This example assumes that you have already set the Voter as the logged-in user within Record Rules.
Click here to learn more about checking for additional fields.
You can create as many validation rules as you need for each form.
Once you create validation rules, you can view the number of validation rules you have for a form within the form's validation rule tab.
We'd love to hear your feedback.