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.
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.
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,
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.
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.
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.
Just in case, since it got ugly when I pasted it before
https://codeshare.io/0gKdNg
Hi Jeff,
I’ve just sent you an email to follow up about this.
Best,