Require an Existing Value for Submission with Gravity Forms

Ensure that a value entered in Form B has been previously submitted (or generated) on Form A. This is useful if you’re generating a reference number of some sort on Form A and would like the user to enter it on Form B.

December 11, 2023: Fixed compatibility issue with Gravity Forms 2.8.

June 22, 2022: Improved support for validating field map via AJAX.

Let’s say a reference number is generated on Form A (using something like GP Unique ID) for the user’s order (represented by a Gravity Form entry). The reference number is emailed to the user.

On Form B, the user is able to contact you about their order by entering the reference number. If they fail to enter a valid reference number, a validation error is returned and they are unable to submit the form.

validate-that-a-value-exists-for-a-gravity-formThe form will generate this error unless a valid reference number is entered.

Looking for an simple way to generate a reference number for your Gravity Form submissions? GP Unique ID for Gravity Forms can help.

Check out this step-by-step tutorial on how to use GP Unique ID to generate a reference number for your Gravity Form submissions and use this snippet to validate those reference numbers on subsequent submissions.

How to Generate and Validate a Reference Number for Gravity Forms with GP Unique ID

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

    • Once you have installed the snippet, look at the bottom of the snippet for this block of code:

      new GW_Value_Exists_Validation( array(
      	'target_form_id'  => 123,
      	'target_field_id' => 1,
      	'source_form_id'  => 124,
      	'source_field_id' => 1,
      	'validation_message' => 'Hey! This isn\'t a valid reference number.'
      ) );

    • The target form ID and field ID represent the form and field that should be validated and show the validation error when validation fails.
    • The source form ID and field ID represent the form and field that store the existing value that the value submitted in the target form and field should be checked against.
    • The validation message can be updated to any message you would like to indicate that the entered value did not match any existing values in the source form and field.

Parameters

  • target_form_id (integer) (required)

    This is the Form ID for the field that you will checking if a value already exists (Form B in the example above). There is no default value.

  • target_field_id (integer) (required)

    This is the Field ID for the field that you will checking if a value already exists (Form B in the example above). There is no default value.

  • source_form_id (integer) (required)

    This is the Form ID for the field that generated the reference number (Form A in the example above). There is no default value.

  • source_field_id (integer) (required)

    This is the Field ID for the field that generated the reference number (Form A in the example above). There is no default value.

  • validation_message (string) (optional)

    This is the error message that will be displayed if the value being tested does not already exist. Defaults to "Hey! This isn't a valid reference number."

  • field_map (array) (optional)

    An array of target and source field IDs; allows for creating multiple requirements that must match the same entry. Format should be:

    array(
            // SOURCE_FIELD_ID1 => TARGET_FIELD_ID1,
            2 => 3,
            5 => 11
        );

  • disable_ajax_validation (bool) (optional)

    Optionally disable AJAX validation. AJAX validation is enabled by default and will add a checkmark or an x-mark depending on whether the value exists.

  • validate_blank_values (bool) (optional)

    Set to false if you wish to disable validating blank target field values.

Validate phone numbers on your forms in real time with Gravity Forms Advanced Phone Field! This perk also detects country codes automatically, formats numbers accordingly, and provides animated drop down selects (complete with flags 🇺🇸).

Any questions?

This snippet is more of a building block rather than a final solution. Let us know how you’re using it and any questions you have about building on top of it to accomplish a more involved solution.

For an example of how this can be combined with other plugins to create a more robust solution, check out the How to Generate and Validate a Reference Number for Gravity Forms with GP Unique ID tutorial.

We hope you find this snippet useful! If you do, show us some love by sharing this article on the social media platform of your choice. Let’s keep the Gravity Wiz pumping out awesome snippets.

Comments

  1. Jessica Hernandez
    Jessica Hernandez April 14, 2024 at 9:37 pm

    I was so excited to find something that would give me and my team an invite-only solution for an upcoming event. We are allowing additional guests to attend this paid event. For this, we are using Gravity Forms Stripe Add On and GP Auto List Field so that guests can add the names of the additional people they are purchasing tickets for. The Unique ID worked perfectly, but then I noticed that the Auto List Field no longer worked when I increased the tickets on the form. Additionally, the Stripe credit card form stopped working. I was hoping to get this up and running, as this event is on May 31. I hope I can get some feedback here! https://lcosfounders.com/rsvp I should also add that when I turned off the snippet in functions.php, the problem went away.

    Reply
    1. Matt Andrews
      Matt Andrews Staff April 15, 2024 at 9:13 am

      Hi Jessica,

      I see that you’ve also sent us a ticket about this, I’m just letting you know that we’ve replied there.

      Best,

  2. Chris
    Chris January 28, 2024 at 7:13 pm

    Is there a limit on the number of times the referral code can be used? If that is not included in this code, would I need to use a Limit Entries perk or would that work here?

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff January 29, 2024 at 8:05 am

      Hi Chris,

      Limiting the number of times a code can be used isn’t supported with the code snippet. You can however do this with our GP Limit Submission Perk. You can use the field value rule group to limit the number of times a code can be used. The check for this will be done when the form is submitted.

      Best,

  3. Marcel
    Marcel December 20, 2023 at 1:14 pm

    Hello!

    Thanks for the snippet. Can I customize it so that the entry is checked against a column in a custom database?

    The solutions here in the comments from 2015 are no longer available.

    Thanks a lot!

    Reply
    1. David Smith
      David Smith Staff December 22, 2023 at 5:15 pm

      Dang. Looks like that snippet is lost to time. I don’t have a ready solution for this but I bet ChatGPT could whip something up for you. If you’re a Pro customer and prefer to have us write a snippet for you, drop us a line via support.

  4. Alison
    Alison November 3, 2023 at 2:15 pm

    Thank you for such a great snippit! I’ve included the code in a custom plugin I am using for other functions as well. However, I am getting a console error: ncaught ReferenceError: GWValueExistsValidation is not defined

    Can you help me sort that out?

    Thanks!

    Reply
  5. Heather Roberts
    Heather Roberts October 27, 2023 at 11:33 am

    I have a nested form using product/option field combos. I’m trying to make sure the end-user is completing the option field if they select a choice on the product field. I’ve tried conditionally showing HTML fields if option field x is empty. While that does work, the HTML fields don’t disappear when the issue is corrected.

    Is there any other way to make the option field conditionally required if a specific selection is made?

    Reply
    1. Matt Andrews
      Matt Andrews Staff October 27, 2023 at 11:51 am

      Hi Heather,

      I have just sent you an email to follow up about this, as I’d like to get some more information about your specific set up.

      Best,

  6. Manuel Sandoval
    Manuel Sandoval August 26, 2023 at 9:39 pm

    Thank you for this. Assuming an incorrect value is entered and the form is submitted – is there a way to have Gravity Forms identify that as a Submission? I would like to give users 3 tries to enter a correct value, and then prevent them from submitting more entries if they do not enter correctly. I know I can limit submissions in the form settings, but it does not seem like this code actually submits the form, just shows the error.

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff August 28, 2023 at 7:52 am

      Hi Manuel,

      The functionality of the code doesn’t allow the user to submit the form if the value entered is incorrect, so it won’t work for your use case. To get it working the way you want, may require some customization to the snippet. If you have an active Pro license, you can contact us via the support form, so we look into this further.

      Best,

  7. Jeff M
    Jeff M August 23, 2023 at 10:56 am

    I have successfully installed the snippet and everything works great targeting one ‘source_field_id’ to one ‘target_field_id’. My issues arise when attempting to use the ‘field_map’, and I am likely not using it correctly (in all honesty).

    In my original attempt at mapping 1 to 1, this works great:

    ‘target_form_id’ => 11, ‘target_field_id’ => 2, ‘source_form_id’ => 4, ‘source_field_id’ => 98,

    Where mapping field 98 (email address) from form 4 to field 2 (email address) on form 11, again no problem at all.

    What I am attempting to do is take multiple source fields from form form 4. This was a event registration form, so depending on the number of attendees registered I have multiple email address fields. I want the validation to occur across each field, then they can proceed to a page to download documents from the event.

    Here is what I tried:

    new GW_Value_Exists_Validation( array( ‘target_form_id’ => 11, //’target_field_id’ => 2, ‘source_form_id’ => 4, //’source_field_id’ => 98, ‘field_map’ => array( // SOURCE_FIELD_ID1 => TARGET_FIELD_ID1, 98 => 2, 37 => 2, 41 => 2, 45 => 2, 52 => 2, 66 => 2 ), ‘validation_message’ => ‘Hey there! Does not look like you were an attendee.’, ) );

    What happens is in the above, no entered values work. If I comment out all but 1 line in the array, that fields values will validate.

    I am missing something, likely very simple, but for the life of me I cannot figure it out.

    Reply
    1. Dario
      Dario August 23, 2023 at 11:39 am

      Jeff, the snippet setup seems correct. If it is not working for you something else must be going on. You can try by removing from the map attribute 98 and 2 as the field ids since both are already set in the target_field_id and source_field_id attributes.

      If you’re having any difficulties setting this and if you have an active Gravity Perks license, contact us via our support form, so we can look at your setup and assist you further.

    2. Jeff M
      Jeff M August 23, 2023 at 1:02 pm

      Thank you Dario, I have tried your suggestion and am still in the same position. With the field_map function in place it fails to validate against ANY of the source_field_id’s. As soon as I comment all but one of the out (I have tried each one) I can validate an email address for the respective source_field.

      I do consistently see the following in the browser console, though I see it whether this is working or not:

      Uncaught ReferenceError: GWValueExistsValidation is not defined

      Unfortunately I am not currently a Gravity Wiz customer – wonderful product addons for sure. I just have not had a need yet to purchase. I feel though that that is coming soon.

  8. Omar
    Omar May 29, 2023 at 11:06 am

    Hello, I have used it and it work very well for me, but I faced an issue, assuming the source field ID is ‘200’ and the target field ID is ‘222’ the code will be able to validate any values entered in the field 200 without checking the entry ID. because I am using this code for validating a secret code sent to users but I want to make sure that it wont be validated if the user didn’t use the exact value he received.

    Reply
    1. Scott Ryer
      Scott Ryer Staff May 30, 2023 at 1:54 pm

      Hi Omar,

      If the codes are unique per entry, this snippet will validate each unique value for each entry. If you’re using the same code across multiple entries, this snippet will not differentiate between the different entries. If you need a way to generate unique codes per entry, check out GP Unique ID.

    2. Omar
      Omar May 31, 2023 at 10:57 am

      Hello Scott, The code is unique for each entry as I am using GP Unique ID and it is generating a different value each time, but the target field accept any values generated by GP Unique ID for source field.

    3. David Smith
      David Smith Staff May 31, 2023 at 9:35 pm

      Hey Omar, not sure I’m tracking but since you’re a Gravity Perks customer, drop us a line via support so we can dig into the specifics of your configuration.

  9. Jason
    Jason April 11, 2023 at 2:46 pm

    This is a great post, and really helped me out with a client task.

    A heads up I did find one bug, though, having to do with the field_map array:

    The article (and a comment below) describe the syntax of the field_map array as TARGET_FIELD_ID1 => SOURCE_FIELD_ID1, but the correct usage is SOURCE_FIELD_ID1 => TARGET_FIELD_ID1.

    Otherwise, it worked great. Huge thanks to author Mr. Smith and commenter Akter.

    jason

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff April 12, 2023 at 5:57 am

      Hi Jason,

      Thanks for pointing this out. We’ll work on getting the article updated.

      Best,

  10. Matteo
    Matteo January 28, 2023 at 9:52 am

    Hello, I’m trying to understand if GP Unique ID is the right solution (and how) for my needs.

    Let’s say that I have a form which collects: – Name – Email – an optional Referral Code Each submitted entry generates an Unique ID (which can be distributed and then used by others as optional Referral Code).

    Scenario: – UserA submits an entry to the form and gets xxxxxx as Referral Code. – UserA gives xxxxxx to UserB – UserB submits an entry to the form and use xxxxxx as code. – I need that UserA gets an email that confirms that UserB used his code.

    Is that possible? How? Thank you in advance.

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff January 30, 2023 at 8:13 am

      Hi Matteo,

      GP Unique ID would work for the referral code. However, you would also need our GP Populate Anything Perk to populate a Hidden Email Address field on the second form, with User A’s email address matching the referral code entered by User B. You can then set up a Notification to send an Email to User A using the populated Email Address. You will have to set up conditional logic on this notification to send only if the hidden Email Address field is not blank.

      I hope this helps.

      Best,

    1. Brad Osborne
      Brad Osborne September 28, 2022 at 2:44 pm

      Hi Daniel,

      Ok no worries thanks for updating us. You’ve got to watch those sneaky commas :) .

      Cheers,

      Brad

    1. Dario Space
      Dario Space May 4, 2022 at 6:48 pm

      Hi Brian,

      You will need to add two instances of the configuration to validate two fields. In the snippet you’ve sent, there’s only one. If you have an active Gravity Perks license, can you get in touch with us via our support form.

      Best,

  11. Ozz
    Ozz October 25, 2021 at 4:49 pm

    Hi David,

    I would love this exact snippet doing the opposite. If the value entered already exists within all the forms entries, users should get an error message and can’t submit the form until a unique value is entered. Is there a version to do the exact reverse by changing the true and false states? I have tried the limit submissions but it doesn’t pick up previous entry fields?

    Reply
    1. Dario Space
      Dario Space October 27, 2021 at 9:44 am

      Hi Ozz,

      We’ve already followed up on this via email, but I wanted to post this in the comment thread so other customers could see it.

      Limit Submissions Perk should work when using a Form Field value as a limit.

      Best,

  12. Brandon
    Brandon February 25, 2021 at 2:51 pm

    I have a theoretical question. I’m using Gravity Forms for support tickets, but only certain websites are eligible for support. I want to validate their website. Could I create a user registration form (Form A) where they put in their website address and then use that form as a source of validation that their website is covered on Form B? Also, lets say their support coverage ends on that website, would I need to delete the entry in Form A, or could I use the field_map parameter to look at a field in Form A that shows whether that website’s support is active?

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff February 26, 2021 at 3:01 am

      Hi Brandon,

      Your solution looks like a good one and it should work perfectly for what you want to achieve. Form A, which is the user registration form will contain the entries with website addresses that Form B will use to check if a User’s website is eligible for support. Currently, you’ll have to delete an entry if a user’s support coverage ends. However, if you’ll want to keep those entries when the support coverage ends, a workaround will be to manually append a text to the website URL on the Entry page, so that when a user enters it in Form B, there wouldn’t be a match.

      Best,

  13. Marcel
    Marcel January 14, 2021 at 10:02 pm

    Hi,

    How would I go about using the snippet for multiple forms. Example: I want to validate field id 1 in form 1 against field id 2 in form 2, but then I also want to validate field id 1 in from 3 with field id 2 in form 4. So basically more than 1 source id and target id. I tried to copy the code twice in functions.php and renaming the class and some of the functions, but looks like it calls the init of the first.

    Thanks, Marcel

    Reply
  14. Marcel
    Marcel August 23, 2020 at 6:16 am

    Hi,

    this does not seem to work across forms,but only when source and target form ids are the same…. is there a bug?

    Thanks, Marcel

    Reply
  15. churmi
    churmi July 28, 2020 at 5:23 am

    How would I add a date condition to the validation? So not only does the code but it validates the date as well. For example it validates the code and also the date_created against the current date. Where the current date is not more than 3 days from the date_created.

    Anyone have any ideas?

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff July 28, 2020 at 9:19 am

      Hi Churmi,

      This may be possible with some changes to the snippet, but unfortunately, we are unable to assist with snippet customizations at the moment.

      Best,

    1. Ryan Donovan
      Ryan Donovan May 23, 2020 at 4:16 pm

      Hello Jasim, could you please let us know which snippet you are refering to? Thanks!

    2. Ryan Donovan
      Ryan Donovan May 26, 2020 at 11:52 am

      Hello Jasim, I just tested this out on my test server and cannot recreate this issue. Both snippets are working perfectly fine with one another. Are you adding ?form_page=2 at the end of your URL? What seems to be the issue? Could you run a theme/conflict test?

  16. Jerry Lin
    Jerry Lin May 12, 2020 at 6:15 pm

    Will this work to cross validate between forms? Example: Check if name and dob exists from form a to form b.

    Thanks

    Reply
  17. HAMMAL MANSOOR
    HAMMAL MANSOOR July 12, 2019 at 4:23 pm

    Looking for help to use this snippet Hey can you we validate to target ids in the form A with one source id in Form B ? if yes what changes need to be done in the code above?

    Reply
    1. HAMMAL MANSOOR
      HAMMAL MANSOOR July 16, 2019 at 11:01 am

      @David. I mean I want to validate the code in two different places in form A and both the code as the same source Form B how can I do that.

  18. Stephanie Ferreira
    Stephanie Ferreira May 6, 2019 at 2:47 pm

    I can modify for the user only have access to the form if the payment of woocommerce is marked “Completed”?

    Reply
  19. Lakewood Web
    Lakewood Web December 12, 2018 at 6:21 am

    Hi, Thanks for your previous reply. Is it possible to have multiple instances of this snippet e.g. for two separate forms? Can you explain how? Thanks!

    Reply
  20. Lakewood Web
    Lakewood Web December 6, 2018 at 3:56 pm

    1) Same question as Adesola 2) Can I validate against the “Entry ID” field? how do I get the ID of that field? Thanks!

    Reply
  21. Adesola Fakile
    Adesola Fakile February 9, 2018 at 11:33 am

    Hello david, how do I bulk generate unique membership Ids for users that had already submitted entries before the unique ID field was added to the form?

    Reply
  22. Michael
    Michael November 30, 2017 at 10:04 am

    Hi David, I found your script and I wonder if it can be a base for a solution from my task. I tried to customize the script, but it does not work. Is there a chance to help me?

    May task: I have 3 relevant fields: 1: username 2: projectnumber 3: shared username

    If a user creates an entry to be searched if the same number already exists in another entry. If the number already exists the username of the first user should be entered in field 3. The reason is the following: it is allowed to use identical numbers multiple times. but the users should be shown, which user has already used these identical numbers.

    Kind Regards Michael

    Reply
    1. David Smith
      David Smith Staff December 12, 2017 at 8:01 am

      Hi Michael, unfortunately, we just don’t have the throughput to help out with snippet customizations. :/

  23. Ahmed
    Ahmed November 6, 2017 at 7:59 pm

    Hi, Thank you for your great effort. I tried to use it and it did work however when i try to use the field_map, it doesn’t stick to the pointed source_field_id only but it also accepts any entries from other fields ids if the entered value matches. My example below: the field id 12 – form 18 should only validated through field id 5 – form id 17 and the field id 6 – form 18 should only validated through field id 3 – form id 17 but what happens is that if the entered value in field 12 matches an entry value at field id 3 it marks it as valid and continue and same for field id 6 and source field id 5.

    Please help.

    Thanks in advance

    Reply
    1. David Smith
      David Smith Staff November 8, 2017 at 9:23 am

      Trying removing these lines form the configuration:

      ‘target_field_id’ => ” ‘source_field_id’ => ”

  24. Tim Pehrson
    Tim Pehrson September 23, 2017 at 9:07 pm

    Hi, Thanks for the wonderful snippet!

    Question: When validating an ID generated from form A in form B, is it possible to parse another field value from the form A submission with the matching ID?

    For instance: Form A collects an email address, generates an ID and sends it there. When validating ID with your snippet in form B, I’d like to parse the entry with the matching ID for the email address for the purpose of emailing a confirmation.

    I think I can tease out the matching gravity forms entry with this part of the code, but need a little guidance. Thank you!

    Reply
    1. David Smith
      David Smith Staff September 30, 2017 at 1:57 pm

      Hi Tim, this would require some customization of the snippet. Not terribly difficult if you have some PHP experience but more than I can provide instruction on here.

  25. Nilesh
    Nilesh December 12, 2016 at 8:25 am

    Hello,

    I am not getting an validation error when the unique id is submitted to the second form and also the form getting submitted without filling other required fields.

    Thanks in advance

    Reply
    1. David Smith
      David Smith Staff December 19, 2016 at 10:28 pm

      Hi Nilesh, I’ve confirmed this snippet is working (via the demo). First, confirm you’ve installed and configured the snippet correctly (extra tips here). Next, try disabling all other themes and plugins to see if there is a theme or plugin conflict.

    2. Armand
      Armand January 8, 2017 at 12:24 pm

      Nilesh, have you fixed your problem? I have the same issue, i´m not getting validation, have wordpress 4.4.2 and Flat Bootstrap theme Versión: 1.9 by XtremelySocial, perks installed an GP Unique ID activated. The snippet is configured in functions.php file. Please Help

  26. Ike
    Ike October 24, 2016 at 7:37 am

    Hi Dave, thanks for such an awesome solution.

    Can you give an example code in reference to overwriting the “does_value_exist” method to validate/require a field from a database value? Say the meta-key is ‘card_id’ located in the wp_postmeta table.

    Thanks, Ike

    Reply
  27. Akter
    Akter August 27, 2016 at 12:29 am

    Hi David Smith, Nice work. Everything is fantastic. But, It will be better if you describe something about the ‘field_map’ parameter. I guess, you have added this for supporting multiple fields validation at a time. If I am not wrong, this is kinda like this: Values of Fields 1 & 2 of Form B will be validated by Fields 1 & 2 of Form A. Isn’t it? Can you please write some example on how to use this parameter? Should I keep empty on individual target_field_id & source_field_id if I declare them on ‘field_map’?

    Thanks in Advance!

    Reply
    1. Akter
      Akter August 27, 2016 at 3:05 am

      Got the features of ‘field_map’ parameter: Use it like-

      Configuration

      new GW_Value_Exists_Validation( array( ‘target_form_id’ => 22, ‘target_field_id’ => ”, ‘source_form_id’ => 21, ‘source_field_id’ => ”, ‘validation_message’ => array(1=>’Your Username may not satisfy the current policy requirements. Please try carefully!’, 2=>’Your Password may not satisfy the current policy requirements. Please try carefully!’), ‘field_map’ => array(1=>1, 2=>2) //target_field_id => source_field_id, to => from ) );

      Also change this to line 55, if you need different validation message based on field-

      $field[‘validation_message’] = $this->_args[‘validation_message’][$field[‘id’]];

      Cheers!

    2. David Smith
      David Smith Staff August 29, 2016 at 10:58 am

      Glad you were able to get this figured out. I’ve updated the article with a little documentation for the “field_map” parameter.

  28. Jose Aristud
    Jose Aristud June 28, 2016 at 8:42 am

    Hi,

    I want this exact snippet doing the opposite. If the value entered already exists within all entries, user should get an error message and can’t submit the form until a unique value is entered. Thanks.

    Reply
    1. David Smith
      David Smith Staff June 30, 2016 at 8:44 am

      Hi Jose, do you mean you need Gravity Forms “No Duplicates” feature but apply it to all fields of all forms?

  29. Víctor Hugo Jurado
    Víctor Hugo Jurado June 20, 2016 at 4:24 pm

    Hi, i´m using this snipet and it works fine, im validating in form B with a value of form A. but now i have another requirement with the same case: The data validated is an id number in the form A, the field 2 in form A is the name of the user, i need to print that name (from the same entry that the validated number id) in the field after the validated field in form B.

    Anyone knows how to help me with this??

    Reply
    1. David Smith
      David Smith Staff June 20, 2016 at 5:37 pm

      Hi Victor, we do not currently have a solution for this. Let us know if you come up with one. :)

  30. Neil Heird
    Neil Heird March 14, 2016 at 10:45 pm

    Hi Nick. Thanks for the follow up.

    For my specific issue that you responded to – I ended up going in an entirely different direction… now I have a single GF input promo code box that uses conditional confirmations. the default action is an in-valid entry that displays an error message indicating an invalid entry, and then each conditional confirmation is triggered based on the correct promo code being submitted. using the Gravity PDF plugin, I set up a different PDF document for the output associated with each valid entry (because each is different). so, when the right code is entered, the site admin gets an email, the user gets a printable PDF certificate that explains what their discount / incentive is, and the client gets a new email in their CRM and a legitimate follow up opportunity.

    So for this one, I think I’m OK for now. But…. I would like info on any custom work you may offer for extending Gravity Forms!

    Thanks again for the follow up and let me know the best way to ask about some special needs I currently have.

    :)

    Reply
  31. Neil Heird
    Neil Heird January 4, 2016 at 12:36 pm

    Hi. I love Perks and your great library of snippets!

    A question I have relates to validating an existing code – not one generated by a previous form.

    so I was wondering… can I use a form to create a specific unique ID (lets call this form A). then direct customers to my site to input a pre-set unique code (created using form A) on a new form (form B). Why do I need this? because have advertised a specific promo code of ABC123 in a magazine, and different code of DEF456 in another. I want to specify pre-determined promo codes for tracking purposes, while preventing random entries from successfully submitting the form.

    make sense?

    Reply
  32. Nick
    Nick December 3, 2015 at 9:56 am

    Hello, I have this feature working, however, I was hoping you could help me get to the next step.

    After the user has put in the correct validation code, I need to redirect them back to the form submission for which the validation code was generated.

    To clarify: User completes form A User receives confirmation with validation code User goes to form B User enters validation code User is taken back to view what was submitted on form A

    Thank you in advance for any help. Nick Brown

    Reply
  33. Weekly Roundup: January 23, 2015 – Sell with WP November 18, 2015 at 2:41 am

    […] Gravity Wiz posted a couple of new Gravity Forms tutorials this week: how to generate and validate a reference number from your form, and how to require this value to submit another form. […]

    Reply
  34. Juan Carlos
    Juan Carlos October 27, 2015 at 10:04 am

    Hello David, I’m sorry to keep bothering you but I really have tried to adjust your snippet to use more source forms than just one but it even failed with only two. Please, let me know if you can help.

    Juan Carlos

    Reply
  35. Robert de Leeuw
    Robert de Leeuw October 25, 2015 at 7:53 pm

    Hi Wiz,

    Great product! Love your work. But I have this strange problem with the number field. When I use the random number generator and I get for example 0001, the number field removes all the beginning 000’s. Is there any way I can prevent the number field to remove the first 0, or is the only solution using a text field?

    Thanks for your support!

    Robert

    Reply
  36. Juan Carlos
    Juan Carlos October 22, 2015 at 9:05 pm

    Hello David, Thank you very much for the awesome snippet but I do require to look the “source_field_id” from several forms (15 in total). I did look at https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/

    I tried this:

    foreach( array( 17, 31, 46 ) as $form_id ) {

    new GW_Value_Exists_Validation( array( ‘target_form_id’ => 56, ‘target_field_id’ => 1, ‘source_form_id’ =>$form_id, ‘source_field_id’ => 11, ‘validation_message’ => ‘Your e-mail does not exist in none of our forms.’ ) );

    }

    but did not work. Could you please show me the right direction?

    Thank you in advance.

    JC

    Reply
    1. Juan Carlos
      Juan Carlos October 23, 2015 at 8:46 am

      Hi David, Thank you for replying. When I say it does not work I mean that, the way I understand, is that when you provide all the source_form_id in the array it should look trough all forms into the source_field_id (which is the same for all 15 forms).

      I put an email for that exists in one of the forms, but it does not find it. Therefore the email is not saved into the new target form.

      How do you make sure that it is looping trough all the forms searching for the source_field_id in the array?

      Thank you.

  37. jen
    jen September 22, 2015 at 4:09 am

    Hi!

    Is there a way to validate 2 fields? Like a combo of a serial number and dropdown list?

    So when I validate, the serial number must match the option from the dropdown from the entry?

    Thank you very much!

    Reply
    1. David Smith
      David Smith Staff September 22, 2015 at 8:11 am

      Hi Jen, do you mean:

      a. Validating a serial number on Form B against a drop down on Form A? b. Validating a serial number on Form A against a drop down on Form A?

      The answer to (a) is yes, this is possible with this snippet currently. The answer to (b) is also yes, but would require additional custom code. Here’s a tutorial on implementing custom validation:

      https://www.gravityhelp.com/documentation/article/using-the-gravity-forms-gform-validation-hook/

    2. jen
      jen September 22, 2015 at 6:41 pm

      Hi Greg!

      what i meant was, both serial number and dropdown options in Form B to be validated from Form A.

      I have managed to use this to validate the serial number, but how to get the dropdown options?

      The combination of number + option entered in Form A must match when you enter a value in Form B.

      Hope I make sense. thank you so much :)

  38. Giacomo
    Giacomo September 11, 2015 at 11:50 am

    Hi David, i’d like to sell the refernce number. So what you suggest me? if i use, for exemple, woocommerce, i can pass the number sell on the form created with gravity form? There is a way to sell a code, like a password for a premum content?

    Reply
    1. David Smith
      David Smith Staff September 19, 2015 at 7:37 pm

      Hi Giacomo, there are many questions that would need to be answered before I could offer any useful advice on this. I would suggest hiring a developer to assist with this.

  39. Mariano
    Mariano August 27, 2015 at 8:36 am

    Any way to check two variables in order to validate an entry? I would like to get the same result as in user/password validation (but not to create a new wordpress user for each one)

    Example: Field User + Field Password Check if user is valid from a list (this script already does that). Check if the password is valid for that user.

    Any idea?

    Reply
  40. Jessica
    Jessica August 13, 2015 at 10:00 pm

    I’m trying to do what Greg was inquiring about, but I’m not familiar enough with PHP to integrate the CSV reference. What am I missing?

    Reply
    1. jessica
      jessica August 28, 2015 at 10:04 am

      Thanks Greg,

      I picked up the Gravity Forms Entry Importer, and have my CSV uploaded to the form field. Unfortunately, I’m not having luck getting it all to work. I am testing this with a code that I know isn’t on the list, and it’s giving me the validation error message (which is good, yay!), but when I test it with a code that I know is on the list, I get the same validation error.

      Here’s what my code looks like:

      Configuration

      new GW_Value_Exists_Validation( array( ‘target_form_id’ => 1, ‘target_field_id’ => 3, ‘source_form_id’ => ‘/RSVPcodes.csv’, ‘source_field_id’ => ‘RSVP Code’, ‘validation_message’ => ‘Hey! Don\’t be a villain! Provide a legit RSVP code to reserve your spot at the Smackdown.’ ) );

      In this instance 1 refers to the form ID, 3 is the field ID, /RSVPcodes.csv is my database reference, and RSVP Code is the name of the column with my data. Do I need to edit the code elsewhere? Is this code incorrect? Any help would be greatly appreciated.

      See it all in action here: http://www.bbgraphics.com/halloween/index.php/rsvp/

  41. Martin Lubran
    Martin Lubran June 10, 2015 at 11:55 am

    Can I use the code twice in the same functions.php file to perform two validations by only changing the parameter values?

    Reply
  42. Massimiliano Mordini
    Massimiliano Mordini May 14, 2015 at 5:06 pm

    Hello and thanks for the awesomeness! This is seriously cool and alone is worth the license! :) Now please, go that extra mile to save my day! :D

    I need a help: I’d like to use this system WITH A CONDITION. The logic would be:

    IF form field X is VALUE, skip the validation; ELSEIF form field X is ANOTHER VALUE, validate.

    The reason for this request is that the user is prompted with a yes/no question. If they say yes, I need to check if they have a code; if they say no, I don’t need to check anything and the field containing the code to validate doesn’t even appear.

    But, unfortunately, if I don’t find a way to skip validation, the hidden field will ALWAYS output an error. (and, no, I can’t work around it by setting a default value, as I need “no-duplicates” option).

    I have an understanding of PHP, but I’m no expert. I’d probably find a solution, given enough time, but I’d LOVE to avoid all the headache and get the correct answer asap. ;)

    Regards Max

    Reply
    1. David Smith
      David Smith Staff May 14, 2015 at 9:15 pm

      Hi Massimiliano, I’ve just updated the snippet so that if a field is hidden via conditional logic it will no longer be validated by this snippet. :)

    2. Massimiliano Mordini
      Massimiliano Mordini May 15, 2015 at 3:09 am

      I had no idea the solution could be so beautifully simple. :) Your solution was even better than my proposal, so kudos to you! ^_^

      Now the concept is really complete, allowing for example to create custom registration forms in GF, where the user can choose the level and is requested a code accordingly.

      Coupled with a decent membership plugin, this is a real jewel! Give a special membership level to buyers, another to cooperators, another to… I don’t know… your grandparents, each one protected by a different password, or set of validation codes.

      I’ve struggled to achieve something similar for a long time, but it was too niche to find a tutorial, and not important enough to justify hiring a more experienced person.

      As far as I know, there’s no other way in wordpress to do similar things which is at the same time:

      • easy
      • relatively cheap
      • quick
      • almost code-free, and where the code is simple

      You might want to consider evolving this into a Perk, maybe with a backend page to handle the configuration and adding some options (although I consider this version absolutely mature now – maybe validate against a custom db table, as Greg suggested).

      There might not be MANY people out there, with a need for this. But those who are, will definitely love you.

    3. Massimiliano Mordini
      Massimiliano Mordini May 15, 2015 at 3:49 am

      Which actually leads me to ask: how can we set this up so that we can validate as follows?

      FORM_X, FIELD_Y vs FORM_A, FIELD_B (like now) and also FORM_X, FIELD_Z vs FORM_A, FIELD_C ?

      Some guess work to make my question more clear:

      foreach( $result['form']['fields'] as &$field ) {

              if( $field['id'] == $this->_args['target_field_id'] && ! GFFormsModel::is_field_hidden( $result['form'], $field, array() ) ) {
                  $value = rgpost( 'input_' . $field['id'] );
                  if( ! $this->does_value_exist( $value, $this->_args['source_form_id'], $this->_args['source_field_id'] ) ) {
                      $field['failed_validation'] = true;
                      $field['validation_message'] = $this->_args['validation_message'];
                      $result['is_valid'] = false;
                  }
              }
      
                         elseif( $field['id'] == $this->_args['second_target_field_id'] && ! GFFormsModel::is_field_hidden( $result['form'], $field, array() ) ) {
                  $value = rgpost( 'input_' . $field['id'] );
                  if( ! $this->does_value_exist( $value, $this->_args['second_source_form_id'], $this->_args['second_source_field_id'] ) ) {
                      $field['failed_validation'] = true;
                      $field['validation_message'] = $this->_args['validation_message'];
                      $result['is_valid'] = false;
                  }
              }
      
          }
      
          }
      

      Would something like this (probably not exactly like this) work to make both validations possible? I’m assuming I’d add more source_form_ids and source_field_ids as needed.

      Otherwise I could save different validation functions in separate custom plugins… no problem about that. Any caveat you wish to share for this solution?

      Thanks in advance Massimiliano

  43. Greg
    Greg March 18, 2015 at 3:57 pm

    Could this be used to validate/require a field from a database value? Looking for a way to have users enter a unique product serial number that we have already entered as either a custom table value or as a taxonomy value.

    Reply
    1. Massimiliano Mordini
      Massimiliano Mordini May 14, 2015 at 5:11 pm

      I have a similar need and I very humbly suggest you my planned workaround for this problem: bulk import form entries from a csv file.

      Might not be the most elegant solution, but at least you’d keep the custom code and the hassle to a minimum.

      Anyway, considering when you posted, I hope you found a solution already. ;)

    2. David Smith
      David Smith Staff May 14, 2015 at 9:17 pm

      Missed your comment, Greg. Sorry about that. Massimiliano’s suggestion is a viable, custom-codeless solution. If you’re comfortable dipping your toes in the code, you could just overwrite the “does_value_exist” method in the snippet to query your custom table instead of GF entries.

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

Require an Existing Value for Submission with Gravity Forms

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