This page may contain affiliate links. Please read my disclaimer for more information.

Gravity Forms Field Validation Based on Form Entries

  |  by Chris Eggleston

Problem

Recently a client reached out with a very specific request for a project she was working on. Simply put, she needed a way to validate a field input against a list of employee badge numbers.

a) add a field that requires an active badge number to prevent random member signups (easy, yes), but b) somehow validate that field against a pre-populated list to prevent random weirdos from inputting BS badge numbers from getting login access.

The challenge with this is providing an easy way for the site owner to maintain the list or database of badge numbers without relying on a developer to edit code or having to use a separate system with a clunky integration.

Solution

Initially, my thought was to just use the gform_field_validation filter and the example to prevent submission based on a word list, but this wouldn’t provide an easy-to-manage database or list for the website owner to manage.

To solve that problem we would use the entries from another form as the badge database. This way, when the site owner needs to remove a badge from the system they can delete the entry and when they need to add a new badge they simply submit the badge database form.

Once that is set up we can then create a custom function, still using the gform_field_validation filter, to validate the badge number against the entries from the badge database form.

This is the code we used to make this work:

// Badge Validation For Gravity Forms
add_filter('gform_field_validation_3', function ($result, $value, $form, $field) {
    // The ID of the badge number field in Form 3 (replace with actual ID)
    $badge_number_field_id = 6;

    // Only validate the badge number field
    if ($field->id != $badge_number_field_id) {
        return $result;
    }

    // Badge database form ID
    $badge_database_form_id = 5;

    // Get all entries from the badge database form
    $entries = GFAPI::get_entries($badge_database_form_id);

    // Extract all valid badge numbers
    $valid_badges = [];
    foreach ($entries as $entry) {
        $valid_badges[] = $entry['6']; // Replace with the actual field ID from Form 5
    }

    // Check if the submitted badge exists in the valid badge list
    if (!in_array($value, $valid_badges)) {
        $result['is_valid'] = false;
        $result['message']  = 'Invalid badge number. Please enter a valid one.';
    }

    return $result;
}, 10, 4);

Now, when someone submits the form, the badge number entered is validated against the entries from the badge database form and if it doesn’t match a validation error message is displayed, “Invalid badge number. Please enter a valid one.”

How to use this code

Within the code are comments to guide you on which IDs need to be changed and which form and fields they should be mapped to.

Once you have the correct form and field IDs in place, you will need to copy the entire code snippet and place it in your themes functions.php file or a code snippet plugin like WPCodeBox.

Need More Help?

If you still need help, feel free to use the comments or chat options below.

If you have an active Gravity Forms license, you can also, open a support ticket here.

Photo of author
About the Author
Chris Eggleston
Husband to 1. Father to 4. Grandpa to 1. Gravity Forms enthusiast. As the owner of WP Mantis, Iā€™m on a mission to simplify the WordPress experience for site owners. I try to bring a unique perspective to the Gravity Forms community.

Support Chris - Donate $5

Gravity Wiz Add-Ons

Advertisement

5 1 vote
Article Rating
Subscribe
Notify of
guest
2 Comments
Inline Feedbacks
View all comments
Dave
4 days ago

Love this new problem/solution format, Chris!

We solved this same problem ourselves. The name could probably use some work. šŸ˜†
https://gravitywiz.com/require-existing-value-submission-gravity-forms/