Using Gravity Forms ‘gform_field_validation’ PHP action

The gform_field_validation filter in Gravity Forms allows you to create custom validation logic for a field.

Usage

You can use this filter for all forms and all fields like so:

add_filter( 'gform_field_validation', 'your_function_name', 10, 4 );

You can target all fields in a specific form:

// This targets all fields in form 6
add_filter( 'gform_field_validation_6', 'your_function_name', 10, 5 );

Or you can target a specific field in a specific form:

// This targets field 1 in form 6
add_filter( 'gform_field_validation_6_1', 'your_function_name', 10, 5 );

Parameters

  • $result (array): The validation result to be filtered. For example: array( ‘is_valid’ => false, ‘message’ => ‘Please enter a number greater than 10’ )
  • $value (string | array): The field value to be validated. Multi-input fields like Address will pass an array of values.
  • $form (Form Object): Current form object.
  • $field (Field Object): Current field object.
  • $context (string): The context for the current submission. Available since Gravity Forms 2.6.3.2. Possible values: form-submit, api-submit, or api-validate.

More information

See Gravity Forms Docs: gform_field_validation

Examples

Number Field Validation

This example validates a field so that only numbers less than 10 are allowed.

add_filter( 'gform_field_validation_175_1', 'custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {
    if ( $result['is_valid'] && intval( $value ) > 10 ) {
        $result['is_valid'] = false;
        $result['message'] = 'Please enter a value less than 10';
    }
    return $result;
}

User Defined Price Field Validation

This example validates a User Defined Price field to make sure the user entered an amount of at least $0.50.

add_filter( 'gform_field_validation_96_23', 'gf_user_defined_price_validation', 10, 4 );
function gf_user_defined_price_validation( $result, $value, $form, $field ) {
    $number = GFCommon::to_number( $value, '' );
    if ( $result['is_valid'] && $number < 0.50 ) {
        $result['is_valid'] = false;
        $result['message'] = 'You must enter at least $0.50';
    }
    return $result;
}

Address Field Validation

This example validates the address field to control which parts are required. Only the street, city, and state are required in this example.

add_filter( 'gform_field_validation_362_1', 'custom_address_validation', 10, 4 );
function custom_address_validation( $result, $value, $form, $field ) {
    if ( 'address' === $field->type && $field->isRequired ) {
        // Check to see if the values you care about are filled out
        $required_inputs = array(
            '1' => $street,
            '3' => $city,
            '4' => $state
        );
        $empty_input = false;
        foreach ( $required_inputs as $input_number => $input_value ) {
            if ( empty( $input_value ) && ! $field->get_input_property( $input_number, 'isHidden' ) ) {
                $field->set_input_validation_state( $input_number, false );
                $empty_input = true;
            }
        }
        if ( $empty_input ) {
            $result['is_valid'] = false;
            $result['message'] = empty( $field->errorMessage ) ? 'This field is required. Please enter at least a street, city, and state.' : $field->errorMessage;
        } else {
            $result['is_valid'] = true;
            $result['message'] = '';
        }
    }
    return $result;
}

ZIP Code Validation

This example validates the zip input of field 1 on form 2.

add_filter( 'gform_field_validation_2_1', 'custom_zip_validation', 10, 4 );
function custom_zip_validation( $result, $value, $form, $field ) {
    if ( $result['is_valid'] ) {
        $acceptable_zips = array( '123', '456', );
        $zip_value = rgar( $value, $field->id . '.5' );
        if ( ! in_array( $zip_value, $acceptable_zips ) ) {
            $field->set_input_validation_state( 5, false );
            $result['is_valid'] = false;
            $result['message'] = 'Zip validation failed.';
        }
    }
    return $result;
}