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.
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( // 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.
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 47+ essential add-ons for Gravity Forms with support you can count on.
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.
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.
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.
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.
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
Hi Jason,
Thanks for pointing this out. We’ll work on getting the article updated.
Best,
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.
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,
When I add this to my function.php the site won’t load
Neverminded, I figured it out. Accidentally added a comma
Hi Daniel,
Ok no worries thanks for updating us. You’ve got to watch those sneaky commas :) .
Cheers,
Brad
Validating a single field is working as expected, but I need some basic help in validating two fields. I’m not sure how to set up the array. The single field validation configuration: http://snippi.com/s/y7c5deo
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,
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?
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,
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?
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,
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
Hey Marcel, check out our article here https://gravitywiz.com/documentation/apply-class-based-snippet-different-forms/ on how to add multiple instances to your same function. Let us know if you have any other questions.
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
Hello Marcel, make sure you’ve installed it correctly to start: https://gravitywiz.com/documentation/snippet-troubleshooting/ The demo is still working so this is likely a configuration issue. If you are still having the issue let us know what version of WordPress and Gravity Forms you are using.
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?
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,
while enabling this script, the “Skip Pages on Multi-Page Forms” script is not functioning
Hello Jasim, could you please let us know which snippet you are refering to? Thanks!
While adding “Multi Page Auto Advance for Gravity Forms” https://gravitywiz.com/require-existing-value-submission-gravity-forms/ snippet to the theme config.php, the “Skip Pages on Multi-Page Form” https://gravitywiz.com/pro-tip-skip-pages-on-multi-page-forms/ snippet stop working
sorry function.php – not config.php
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?
Will this work to cross validate between forms? Example: Check if name and dob exists from form a to form b.
Thanks
The snippet is not working for me and I am unable to determine why. HELP
Hi Kimberly, make sure you’ve installed it correctly to start: https://gravitywiz.com/documentation/snippet-troubleshooting/ The demo is still working so this is likely a configuration issue.
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?
Not sure I understand, Hammal?
@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.
I can modify for the user only have access to the form if the payment of woocommerce is marked “Completed”?
Hm, I don’t have a ready solution for this one.
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!
1) Same question as Adesola 2) Can I validate against the “Entry ID” field? how do I get the ID of that field? Thanks!
Looks like you can just set “id” as your “source_field_id” parameter. Something like this worked for me: http://snippi.com/s/46u74sr
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?
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
Hi Michael, unfortunately, we just don’t have the throughput to help out with snippet customizations. :/
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
Trying removing these lines form the configuration:
‘target_field_id’ => ” ‘source_field_id’ => ”