December 5, 2022: Added support for requiring unique values in List field columns.
September 23, 2022: Added support for case-insensitive comparisons when evaluating whether values are unique.
September 14, 2021: Migrated snippet to the Snippet Library.
January 11, 2020: Added support for requiring unique filenames in File Upload fields (single files only).
November 18, 2019: Added support for experimental "mode" parameter. Defaults to "collective". Also accepts "individual" to require each value to be unique when validating a multi-value field.
December 10, 2018: Fixed issue when comparing values of Checkbox fields (and other multi-input field types).
October 28, 2017: Added support for new parameter "validate_all_fields". Will force the "master" field to be validated along with any other fields in the validation group.
Have you ever needed to collect a personal phone number and an emergency phone number? Or maybe you need to collect a minor’s email address and also their parent/guardian’s email address? It’s important for these values to be unique. You don’t want users entering the same phone number or email address for both fields.
This snippet allows you to require the user to enter a unique value in each of the designated fields. You can specify multiple “groups” of fields
Getting Started
Check requirements
- Make sure you have Gravity Forms installed and activated.
- Already have a license? Download Latest Gravity Forms
- Need a license? Buy Gravity Forms
- Make sure you have Gravity Forms installed and activated.
Install the snippet
- Copy and paste the entire snippet into your theme’s functions.php file.
Configure the snippet
- Enter your form ID for the
form_id
parameter. - Enter each field ID that should be unique for the
field_ids
parameter. These field IDs should be passed as an array. - For more details on available parameters, see below.
- Enter your form ID for the
Usage Examples
Add a Custom Validation Message
new GW_Require_Unique_Values( array(
'form_id' => 12,
'field_ids' => array( 14, 15 ),
'validation_message' => 'My custom validation message!'
) );
Create Multiple Unique “Groups” on the Same Form
new GW_Require_Unique_Values( array(
'form_id' => 2,
'field_ids' => array( 4, 5 )
) );
new GW_Require_Unique_Values( array(
'form_id' => 2,
'field_ids' => array( 7, 8 )
) );
On some forms you may have multiple groups of fields that should be unique (i.e. two sets of phone number fields and two sets of email fields). You can create multiple instances of this snippet to create multiple unique groups.
Require Unique Field Compared Against ALL Form Fields
new GW_Require_Unique_Values( array(
'form_id' => 2,
'field_ids' => array( 7 )
) );
In some cases, you may want a field on the form to be completely unique from any other field on the form. Rather than specifying all field IDs, you can simply specify the single field ID that should be unique.
Require Unique Values in List Field Columns
new GW_Require_Unique_Values( array(
'form_id' => 2,
'field_ids' => array( 9 )
) );
If you specify a single field ID and that field is a List field, this snippet will automatically require that all columns contain unique values. For example, if two rows had the value “Bilbo Baggins” in the first column, a validation error would be returned.
Parameters
form_id (int) (required)
The form ID of your form.
field_ids (array) (required)
An array of fields IDs that should be unique.
validation_message (string) (optional)
The message that is displayed if the field values are not unique. Defaults to:
'Please enter a unique value.'
.validate_all_fields (bool) (optional)
By default, the first field ID in the “field_ids” array is used as the “master” value and not validated. Enable this option to validate the the first field ID as well. Defaults to
false
.case_sensitive (bool) (optional)
Indicate whether the unique value comparison should be case-sensitive. Defaults to
false
.
How’d we do?
We’d love to hear what you think of this snippet. Did you run into any issues installing it? Let us know in the comments below.
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 46+ essential add-ons for Gravity Forms with support you can count on.
Hi. Is it posible to use this to compare 7 diferent number fields and send a notification with conditional logic to the lowest number of the 7 fields?
Hi Martin,
I doubt this tutorial will work for your use case. However, this may be possible with some of our other Perks. Since you’re an active Gravity Perks customer, I’ll email you to request more information on your use case so we can assist you with a solution.
Best,
Hi this snippet was working fine But recently it not functioning as before as the unique value not detected its pointing to the first entered data field
I configured this by swapping the files values. but its not working now
eg ‘field_ids’ => array( 5, 12), – The error will point to field id 5 ‘field_ids’ => array( 12, 5), – The error point to field id 12
any idea?
Hi James,
I just tested the snippet and it works as expected. If you have an active Gravity Perks license, you can get in touch with us via our support form so we can take a closer look at your form setup.
Best,
Can we use an array for form_id to target multiple forms, but with the same field_ids?
Hi Filip,
We’ve sent you a reply via email, you will need to add a unique Configuration for each form.
Best,
I tried to install the snippet but it seems not to work
You can see theme’s function.php here http://snippi.com/s/wnmgiwe
and here the URL where I try to use it https://www.nextvintagereserved.it/#come Thanks in advance for the help
Hi Giuseppe,
The values used for the parameters in the configuration are wrong. You’re supposed to use the Field ID and not the HTML ID. So the form ID should be 1 and the Ids of the fields in the array should be 5, 12, 17, 18, 21. So you would have something like so
new GW_Require_Unique_Values( array( 'form_id' => 1, 'field_ids' => array( 5, 12, 15, 17, 18, 21), 'validation_message' => 'Occorre inserire email differenti per ciascun invitato', 'validate_all_fields' =>true
) );
Best,
Hi David!
I had an unrelated error on my site today, so I enabled the WordPress Troubleshooting from health check to hunt it down. That starts with all the plugins deactivated. In this case, I use the plugin that you provide for this, rather than installing the code in my functions.php.
But I still call the class from functions.php: new GW_Require_Unique_Values
When I entered troubleshooting mode, of course I received a fatal error: PHP Fatal error: Uncaught Error: Class ‘GW_Require_Unique_Values’ not found in… functions.php:230
I knew that I could get around that by installing the code, but then when I exited troubleshooting mode, I received the fatal error because now the class was being created or called twice (once form the plugin and again from functions.php).
I’m sure this is a rare problem. But which file should I use for calling the class so that I do not get these errors when I jump in or out of troubleshooting mode? I know I could ditch the plugin and just install it all straight to functions.php, but then I miss out on updates.
Thanks! Greg
Hi Greg,
Since you have the plugin version installed and have the configuration in the function.php, I’ll suggest you comment out the configuration anytime you want to run the conflict test.
Best,
I’m using the WordPress plugin. Does it still require a snippet? I can’t find the specific snippet if that’s the case.
Hi Dirk,
Yes, you have to configure the snippet with the Form ID and Field ID’s: https://gravitywiz.com/gravity-forms-require-unique-values-for-different-fields/#parameters.
Hope this helps!
Best,