Use Validation Rules to Ensure Unique Votes by Different Users

Validation rules allow you to check the validity of data based on the criteria you set. For instance, you can create a validation rule stipulating that if a form is submitted with a date prior to today's date then the form is invalid, a custom error message will be displayed, and the form record will not be submitted. 

Another instance where validation rules would be highly effective is with voting functionality. Say you have a number of feature requests that your users are asking you to provide on your app and you would like to see what the most popular features requests are so you can best allocate your resources. One way to accomplish this would be by allowing your users to vote on the new features they want. The only problem with this is that you would want to ensure that a single user can not vote more than once on a specific feature, while still allowing them to vote on different features.\

By using Validation Rules this can all be accomplished. As validation rules will be a key part of this tutorial, if you are not familiar with validation rules you can check out our Unique Record Validation Document before we begin. 

Now that you have a general understanding of what we're trying to accomplish, let's move on to building our voting app with validation rules to ensure unique votes by different users. 


The first step is to go into the data builder and add two data tables: Features and Votes. In the Votes data table you can add a Date/Time field and name it: Vote Occurred At.

You can then add two connections from the Votes data table; one to Features and one to Users.


Now that we've designed our data structure, our next step is to design how we will display our data in the Page Builder. In the page builder, add a page with a table component and select the New Features data table. Then select to quick add and choose all four options which should consist of: a form to add New Features, a details page, a Votes table within the details page, and a form to add new Votes.



You can now click on the Add New Features Page that is within the Details Page and click on the form to edit. Once in the form, go to Form Rules tab and select Record Rules.

Then select to Update this Record and set two rules:

Set >Voter Name to Auto Logged User

Set >Vote Occurred At to Current Date and Time


Since we're using record rules to define the values of these fields, we can now remove these fields from the form as they are now redundant. To remove these fields, click the delete icon for each:

Once you have added the record rules you can click on Validation Rules.

Click to Add New Rule and 

Set >If Voter Name is Logged in User

Then click to Check Additional Fields and select Feature Name. You can then add a Custom error message of "You've Already Voted" in the message box.


Now you have created the rules necessary to ensure unique votes on your app. To test this feature on your app, first create a user by heading to the Users Tab. You'll need to be logged in to a user in order for the app to know who is voting.


To test your app, simply login, create a new feature, and start voting!