Search code examples
phpwordpressgravity-forms-plugin

can't target gravityform input or field ID in filter


I have a filter/validation script that only accepts emails from a certain domain. This is working pretty good but I need to have it on a specific from input ID:

As per https://docs.gravityforms.com/gform_field_validation/#11-email-validation-by-third-party-api#usage :

You can also target a specific field by adding the form id and field id after the hook name.

//The following declaration targets field 1 in form 6 add_filter( 'gform_field_validation_6_1', 'your_function_name', 10, 4 );

Currently my code looks like this:

add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {
    if ( ! $result['is_valid'] && $field->get_input_type() === 'email' && strpos($field->get_field_input(), 'company.com') === true) {
        $result['is_valid'] = true;
        $result['message']  = '';
    } else {
        $result['is_valid'] = false;
        $result['message']  = 'E-Mail must be a company.com address';
    }

    return $result;
}, 10, 4 );

My form and input IDs are confirmed as 1 and 13, respectively:

enter image description here

When I change the code to the following, the script stops working:

add_filter( 'gform_field_validation_13_1', function ( $result, $value, $form, $field ) {
    if ( ! $result['is_valid'] && $field->get_input_type() === 'email' && strpos($field->get_field_input(), 'company.com') === true) {
        $result['is_valid'] = true;
        $result['message']  = '';
    } else {
        $result['is_valid'] = false;
        $result['message']  = 'E-Mail must be a company.com address';
    }

    return $result;
}, 10, 4 );

Is there another way I need to check my form ID? What is missing here?


Solution

  • From the documentation gform_field_validation_6_1 means that it targets field 1 in form 6.

    You form id is 1 and input field id is 13 so the filter should be gform_field_validation_1_13.