Use Validation Rules to Ensure Unique Votes Per User
Validation Rules allow you to check the validity of data based on custom-defined conditions and disable a form from being submitted if your validation rules are met.
For instance, you can create a validation rule to prevent a form from being submitted when a user attempts to add a job with an end date that starts prior to the start date.
Another instance where validation rules can be highly effective is with voting functionality.
If you have a Feature Voting Platform where users can vote on features, you would 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 logged-in Voter and the selected feature are both not unique. Meaning, if there is already an existing record of this logged-in voter voting for this specific feature, disable the form from being submitted and show an error display message of "You have already voted for this feature!"
Before we discuss how to create this validation rule, let's first discuss the data structure of this Feature Voting app so you can understand which fields to utilize within the validation rule.
Within the Data Builder, you would have three data tables:
- Users data table with users assigned to a Voter user role
- Features data table storing records of features
- Votes data table with two connection fields: one connection to the Features data table (each vote belongs to one feature) and one connection to the Users/Voters data table (each vote belongs to one voter). Within in the Votes data table you can also add fields such as Vote Name and Vote Occurred At.
Once you've built this data structure within the Data Builder, you can create the Voting form within the Page Builder for users to vote on features.
Within the Page Builder, add an Add Vote Form and within the Record Rules of this Add Vote Form, add a record rule to update this vote record and set the Voter to the Auto Logged User and the Vote Occurred At to the current date and time.
Now, when users/voters submit a vote on a feature, their vote record will automatically be updated to the name of the currently logged-in user as well as the current date and time of which the vote was submitted.
Since you are using record rules to auto-save the values of the logged-in voter's name and the current date and time, you can now remove these fields from the form as they will be automatically saved anyway once the form is submitted.
Once you have added the record rule to the form to auto-save the logged-in voter's name and current date and time, you can now create the Validation Rule to prevent the form from being submitted if a voter votes on a specific feature more than once.
To create this validation rule, open the Add Vote Form component and select Form Rules > Validation Rules > Add New Validation Rule button.
Then, create a validation rule to prevent the vote from being submitted if the Voter and selected feature are not unique. Select the Voter field and select "is not unique". Then select the checkbox for Check Additional Fields and select the Feature field to also check if the selected feature is not unique. When users attempt to submit a vote on a feature, this validation rule will check for an existing record of this logged-in voter and this selected feature. You can also create a custom error message should this validation rule be met, such as "You have already voted for this feature!"
Now, when a user attempts to submit a vote for a feature that is not unique for 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.