Gravity Forms: Require Unique Values for Different Fields
An easy way to make sure the user enters unique values in each of the designated fields.
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.
I’ve been testing the snippet using IDs from two Address fields (e.g. 3.1 and 5.1), but the validation check indicates they are not unique, regardless of whether they are the same, or different. Can this be used for fields within the address field? Example is the following configuration:
new GW_Require_Unique_Values( array( ‘form_id’ => 22, ‘field_ids’ => array( 3.1,5.1 ), ‘validation_message’ => ‘Old address and new address are the same. Please update to reflect your address change.’ ) );
Hi Jonathan,
The snippet works with address sub-fields. I’ll be reaching out to you via email to assist you further.
Best,
Hi david, I download plugin and setup everyting. But compare is not working.
Hi Melda,
I just tested the snippet and can confirm that it still works as expected. Please check to confirm if you have the correct parameter set for the configuration. If you have the configuration set up correctly and it’s still not working, please submit a ticket for this via our support form so we can look into this closer.
Best,
Hi, I wanted to modify the code to allow multiple form IDs instead of a single form ID & it doesn’t work. Can you let me know why as the PHP seems to be correct.
new GW_Require_Unique_Values( array( ‘form_ids’ => array( 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ), ‘field_ids’ => array( 21, 12, 32, 49, 51, 73, 75, 77, 79, 81 ), ‘validation_message’ => ‘You have entered this mobile phone number already. Phone entries have to be unique and not duplicated!’ ) );
Hi Michael,
If you want to apply the snippet to multiple forms, you’ll need to create multiple instances of the Class by duplicating the configuration and updating it with the right parameters and values. You can read more about that here: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
Best,
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,
hello,
this is exactly what i need. but it does not work under gravity form 2.5.x – do you have an update on this?
thanks oli
Hi Oliver,
It seems to be working on our end with Gravity Forms 2.5.
If you are a Gravity Perks customer, drop us a line and we’ll be happy to dig into this.
sorry, i now found the configuration part on the bottom (was confused with the value part on top.
thanks for providing this code!
oli
Does this snippet work with List fields? I’ve tried to install on a list field, but it didn’t seem to work.
Hey Cameron, This should work. Could you submit a ticket to our support channel with your use case and form and we would be more than happy to take a look into this for you.
Hi,
This is exactly what I need. Can this be implemented without Gravity Perks plugin?
Hello Deon,
You would only need to implement the snippet into your website. Check out our help article here on how to implement a snippet onto your site.
Hello David,
Is it also possible to get a validation against another form?
We have form 1 with field 1 and form 2 with field 1
The value of form 1 field 1 can’t already have a submission at form 2 in field 1.
Greetings Jeen
Hello Jeen, While looking into this it does not look like we have a ready-made solution for this as it would require a custom snippet. Will note this as a possible addition to this snippet in the future. Thank you.
Thanks so much for this! I would love it if you could create a plugin that does this.
In case you can’t, how do we make this work for several forms? Can we just separate the different form IDs with commas?
Or do we need to copy and paste all of the code? Thanks!
Hello, Of course we can take this as a feature request and possibly build this in the future. 😀 As for adding this snippet to several of your forms, this tutorial may help with that!
We used the code for Create Multiple Unique “Groups” on the Same Form and it works great, however we have a multiple selection field that we want to be unique. For instance, a person enters their country in Field 1 and in Field 2 they enter all of the countries they will be traveling to. We don’t want them to be able to add their own country in Field 2. With the code you provided it works great, but only if they select their own country by itself. If they select multiple countries and it includes their own, then it doesn’t work. Any suggestions?
Hi Kathi, thanks for being a Gravity Perks customer and submitting a support request. We’ll be in touch soon. 🙂
Could this be modified to compare the values in a list: Say something like NET and GROSS where NET must be less than or equal to GROSS.
A developer could write this custom validation pretty easily. I’m not sure you would benefit much from using this snippet as a base.
Hey David! i want to when error happens on this snippet with AJAX option, What is the solution to do this? Thanks
We don’t have an AJAX solution for this.
Is there a way to make this work on a multi-part form? It doesn’t seem to work for me.
I did a local test and it is working on a multi-part form for me. If you’re a Gravity Perks customer, we’ll be happy to provide additional support via the support form.
Hi David! i have two drop down list with same value. i want to no duplicate in second drop down it means: drop down list one have value: AAA then user cant select AAA in second drop down list. can you help me? thanks
This snippet would prevent the user from successfully submitting the form with the same option selected in both but it would not automatically remove or disable the the choice in the second field.
No i dont want remove my two drop box first for Nationality and second for citizenship. i want if user select Italy for Nationality cant select Italy for Second Citizenship. do you help me for solve this? Thanks
Try implementing this snippet, Mostafa.
Hi,
Has this snippet already been updated to work with GF 2.4? If not, when do you expect this update to come? Previously I had an issue with the Limitation snippet at this point, so this time I check first, to prevent a breakdown of the form…
This snippet has not be tested with GF 2.4; however, I don’t see any obvious conflicts. Let us know how it goes. ?
Right now it looks like it does not work, but I’m not sure if this is because of ‘GF 2.4’, or because I apply it to two checkbox fields on which also the ‘Limit Checkboxes’ snippet is being applied.
Hi Gert-Jan, send us a support request.
Hi David is there a way to ensure list fields are unique both on the form and in all previous entries?
I’m not aware of a solution for this, Tony.
Thanks for this code! It works beautifully!
However, I also use Zapier to do other things with my form submissions. Every since I implemented this code, my Admin Field Labels don’t make it through. Any thoughts?
https://www.aphasia.com/caregiver-eligibility-check/
Almost all of the fields in the “Patient Information” section are supposed to have admin field labels.
Hm, are you expecting to see the admin field labels on the frontend?
David
I need to make sure all field selections in a form are unique. How might that work in the snippet?
Thanks
Unique to each other? Or unique to themselves across previously submitted values for the respective field? If the former, just include all the field IDs on your form in the field_ids parameter. If the latter, enable No Duplicates via the field settings for each field.
Hi Author,
I am using this code as per ur instruction but Validation message not changing.
http://www.glass-academy.com/webinars/
From the above url: Email id is unique but custom validation message not working.
Hi Haridasan, I’ve just tested and this snippet is still working for me locally. Do you have a URL to the form and can you share a snippi of your snippet configuration?
Hi David,
Can you please take look on this URL: http://www.glass-academy.com/webinars/
There will be a form.
Please fill that with the following email id: haridasansocialbeat@gmail.com
It shows “This field requires a unique entry and ‘haridasansocialbeat@gmail.com’ has already been used”. which is Default Message. I want to customize it.
I added you code in functions.php
[code redacted]
The form id is 1 and the field id is 2. Not working please help.
Thanks in Advance
Hi Haridasan, this snippet does not let you customize the no duplicates error message. I believe Gravity Forms support will be able to help you with this.
Hello. I am trying to have something similar on a website, but I’m quite the noob when it comes to php and mysql queries… I rely mostly on plugins, but I can’t find anything that can help my case. I have a feeling that this code can be tweaked somehow to fit my scenario. Basically, what I need is to have some values into a database (like a series of random numbers) and a frontend form where a user can input a number. That number must be one from that database, else he would get a “wrong code” message. So it all comes down to cross-checking the user input with the values in the mysql database. I don’t know if I can do something like this with Gravity Forms – maybe with logical condition, but if there are thousands of numbers it would be insane to add condition for each…
Can you please help me with this? Thank you!
Hi Alex, I’d love to add support for this functionality to this snippet. If you are interested in commissioning this functionality, get in touch.
Not sure if I am looking in the right place…. but.
We love Gravity Forms!
Is there a way to make the Advanced “Address” Field unique?
Thanks! Cedric
This is actually possible with the Better Limit Submission snippet. See the “field_group” option under the “limit_by” parameter.
Thank you so much David
Glad to help, Wilfrid. :)
This is exactly what I needed! Thank you so much for all of the great work that you do, and all that you share freely. I am hoping to purchase your Gravity Perks plugin in the next few days!
Glad we could help!
Hi David,
I have used this code to compare two fields but I have a problem wtih the form_id which seems to not be recognized. It seems to be applied in all forms and not only the one selected.
I have changed the id to test. My form_id is 8. http://comitedesfetesdesmanoreys.fr/fete-du-pays/formulaire-dinscription-fete-du-pays/
new GW_Require_Unique_Values( array( 'form_id' => 12, 'field_ids' => array( 66, 53 ), 'validation_message' => __( 'Attention le nombre de viandes ne correspond pas au nombre d\'adultes inscrits !' ) ) );
new GW_Require_Unique_Values( array( 'form_id' => 12, 'field_ids' => array( 66, 63 ), 'validation_message' => __( 'Attention le nombre de desserts ne correspond pas au nombre d\'adultes inscrits !' ) ) );
new GW_Require_Unique_Values( array( 'form_id' => 12, 'field_ids' => array( 67, 73 ), 'validation_message' => __( 'Attention le nombre de viandes ne correspond pas au nombre d\'enfants inscrits !' ) ) );
new GW_Require_Unique_Values( array( 'form_id' => 12, 'field_ids' => array( 67, 74 ), 'validation_message' => __( 'Attention le nombre de desserts ne correspond pas au nombre d\'enfants inscrits !' ) ) );
Thanks for your help.
Valérie
I have changed form on my website test because the code caused bad issues at all other forms on my website.
The url : Formulaire de test
Thanks.
Valérie
Hi David,
I’ve succeeded to resolve my problem adding this function :
public function is_applicable_form( $form_id ) { return $this->_args['form_id'] == $form_id; }
and modifying the validate function like this :
public function validate( $result, $value, $form, $field ) {
Valérie
Awesome, Valerie! I’m glad you were able to figure this out. Thanks for sharing your solution.