Gravity Forms: Require Unique Values for Different 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.

gw-gravity-forms-require-unique-values

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

  1. Check requirements

  2. Install the snippet

    • Copy and paste the entire snippet into your theme’s functions.php file.
  3. 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.

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.

Comments

  1. Michael Power
    Michael Power September 14, 2023 at 9:18 am

    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!’ ) );

    Reply
  2. Martin Ferreira
    Martin Ferreira March 16, 2023 at 6:01 pm

    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?

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff March 17, 2023 at 6:35 am

      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,

  3. James Simpon
    James Simpon September 22, 2022 at 6:07 am

    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?

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff October 3, 2022 at 3:31 pm

      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,

    1. Dario Space
      Dario Space Staff June 23, 2022 at 7:54 am

      Hi Filip,

      We’ve sent you a reply via email, you will need to add a unique Configuration for each form.

      Best,

    1. Samuel Bassah
      Samuel Bassah Staff April 26, 2022 at 7:46 am

      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,

  4. pianogreg
    pianogreg February 8, 2022 at 10:44 pm

    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

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff February 9, 2022 at 4:48 am

      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,

  5. oliver blaum
    oliver blaum May 11, 2021 at 5:33 pm

    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

    Reply
    1. Dario Space
      Dario Space Staff May 11, 2021 at 5:37 pm

      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.

    2. oliver blaum
      oliver blaum May 11, 2021 at 5:41 pm

      sorry, i now found the configuration part on the bottom (was confused with the value part on top.

      thanks for providing this code!

      oli

  6. Jeen Kersten
    Jeen Kersten May 12, 2020 at 7:54 am

    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

    Reply
    1. Ryan Donovan
      Ryan Donovan May 12, 2020 at 2:40 pm

      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.

  7. STEPMaths
    STEPMaths April 10, 2020 at 12:41 pm

    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!

    Reply
    1. Ryan Donovan
      Ryan Donovan April 10, 2020 at 1:02 pm

      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!

  8. Kathi Watts
    Kathi Watts November 7, 2019 at 11:43 am

    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?

    Reply
    1. David Smith
      David Smith Staff November 15, 2019 at 12:53 pm

      Hi Kathi, thanks for being a Gravity Perks customer and submitting a support request. We’ll be in touch soon. 🙂

  9. Michael
    Michael January 17, 2019 at 3:50 pm

    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.

    Reply
  10. Mostafa
    Mostafa December 22, 2018 at 11:04 am

    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

    Reply
    1. David Smith
      David Smith Staff December 23, 2018 at 4:47 pm

      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.

    2. Mostafa
      Mostafa December 23, 2018 at 11:27 pm

      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

  11. Gert-Jan Buth
    Gert-Jan Buth December 10, 2018 at 12:51 pm

    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…

    Reply
    1. David Smith
      David Smith Staff December 10, 2018 at 1:16 pm

      This snippet has not be tested with GF 2.4; however, I don’t see any obvious conflicts. Let us know how it goes. ?

    2. Gert-Jan Buth
      Gert-Jan Buth December 10, 2018 at 2:17 pm

      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.

  12. Scot MacDonald
    Scot MacDonald March 8, 2017 at 2:03 pm

    David

    I need to make sure all field selections in a form are unique. How might that work in the snippet?

    Thanks

    Reply
    1. David Smith
      David Smith Staff March 8, 2017 at 10:23 pm

      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.

    1. David Smith
      David Smith Staff August 24, 2016 at 11:08 am

      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?

    2. Haridasan
      Haridasan August 25, 2016 at 7:00 am

      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

  13. Alex
    Alex August 9, 2016 at 12:41 pm

    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!

    Reply
    1. David Smith
      David Smith Staff August 9, 2016 at 1:29 pm

      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.

  14. Cedric
    Cedric May 11, 2016 at 11:29 am

    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

    Reply
  15. Erin
    Erin August 14, 2015 at 11:48 am

    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!

    Reply
  16. Valérie
    Valérie August 6, 2015 at 2:44 pm

    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

    Reply
    1. Valérie
      Valérie August 18, 2015 at 3:41 pm

      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 ) {

          if ( ! $this->is_applicable_form( $form['id'] ) || ! $this->is_applicable_field( $field ) ) {
              return $result;
          }</code>
      

      Valérie

    2. David Smith
      David Smith Staff August 18, 2015 at 4:17 pm

      Awesome, Valerie! I’m glad you were able to figure this out. Thanks for sharing your solution.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Download Snippet

Gravity Forms: Require Unique Values for Different Fields

This field is for validation purposes and should be left unchanged.