June 22, 2022: Improved support for validating field map via AJAX.
June 2, 2022: Changed behavior to validate blank target values by default. This can be changed using the
validate_blank_values
parameter.July 16, 2021: Fixed a compatibility issue with GP Populate Anything.
March 11, 2020: Fixed issue where target and source field were mixed up. How did this bug exist for so long!?
October 5, 2019: Fixed issue where AJAX validation was not working correctly when specifying fields via the
field_map
property.February 7, 2017: Fixed issue where submitting the form before AJAX validation had completed resulted in a failure to capture the target field's value.
February 6, 2017: Fixed two issues with validation; validation did not work correctly on submission; AJAX validation was not validating against the correct source field ID.
February 5, 2017: Added support for AJAX validation; value is validated as soon as the user exits the input.
August 29, 2016: Updated article with documentation for "field_map" parameter.
May 14, 2015: Updated does_value_exist() method to only query active entries.
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.
How to Generate and Validate a Reference Number for Gravity Forms with GP Unique ID
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
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( // TARGET_FIELD_ID1 => SOURCE_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.
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.
Did this resource help you do something awesome with Gravity Forms? Then you'll absolutely love Gravity Perks; a suite of 45+ essential add-ons for Gravity Forms with support you can count on.
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!
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.
I do have some PHP experience and a lot of JS. Any help greatly appreciated — thank you!
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
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.
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
Hi Armand, if you’re running GP Unique ID, drop us a line via support. We’ll be happy to help you figure this out.
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
Hi Ike, something like this: http://snippi.com/s/uudsqa8
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!
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!
Glad you were able to get this figured out. I’ve updated the article with a little documentation for the “field_map” parameter.
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.
Hi Jose, do you mean you need Gravity Forms “No Duplicates” feature but apply it to all fields of all forms?
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??
Hi Victor, we do not currently have a solution for this. Let us know if you come up with one. :)
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.
:)
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?
Hi Neil, it’s been a while, but if you’re still in need of this functionality, I’d love to work with you. Get in touch.
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
Hi Nick, I don’t know of a solution for this right now. It might be possible to configure with Gravity Flow.
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
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
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
When you say it does not work, could you clarify on what you are experiencing?
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.
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!
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/
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 :)
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?
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.
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?
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?
Hi Jessica, you can pick up a copy of Gravity Forms Entry Importer to handle the CSV to Entry conversion.
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/
Can I use the code twice in the same functions.php file to perform two validations by only changing the parameter values?
Hi Martin, absolutely. Here’s more info: https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/
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
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. :)
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:
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.
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 ) {
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
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.
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. ;)
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.