Check out our growing library of code snippets for Gravity Forms!
Validate form for no duplicate values across X, Y, Z fields
This snippet ensures that specific groups of fields within a Gravity Forms form do not contain duplicate values. It allows you to define multiple sets of fields for validation, ensuring that no two fields within a group share the same value. If duplicates are found, a custom validation error message is displayed for each affected field.
In the $duplicate_field_ids at the top of the snippet you specify all the form and field id’s.
The number in the square brackets [ ] after $duplicate_field_ids is the form id. For each form you want to do this for you would have another entry of $duplicate_field_ids[formID] = [ ] with all the configuration of the field ID’s as in the first. The snippet contains an example of 2 forms with the specified fields. Each group of fields that should not have duplicate values should be specified in it’s own sub array in the format of [ 21 => 'message', 23 => 'message', 27 =>'message' ], with the numbers being the field ID and the message being the validation error you want to display to the user. If any of the fields in a group have duplicate values validation will fail. In the above example if field 21 and 23 have duplicate values even if field 27 does not validation will fail. You can include the same field in multiple groups if you wish. In that case if it has a duplicate value of a field in both of its groups only the message from the last group in the array it is in will be displayed.
Features
✔ Prevents Duplicate Values – Ensures that no two fields within specified groups have the same value.
✔ Supports Multiple Field Groups – Each form can have multiple sets of fields that must be unique within their group.
✔ Custom Error Messages – Define a specific validation error message for each field when duplicates are detected.
✔ Works Across Multiple Forms – The validation can be applied to different forms with custom field configurations.
✔ Supports Multi-Input Fields – Handles complex field types such as Name, Address, and Checkbox fields.
✔ Optimized Duplicate Detection – Uses efficient logic to check and flag duplicate values only when necessary.
This snippet is particularly useful for scenarios like ensuring unique user IDs, email addresses, product SKUs, or any other critical values in Gravity Forms.
This code is best installed directly on your site using the Code Snippets plugin.
Loading...
Review this entry
You must be logged in to post a comment.