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 47+ essential add-ons for Gravity Forms with support you can count on.
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.