Using Gravity Forms ‘gform_form_validation_errors’ PHP action

The gform_form_validation_errors filter allows you to modify or override the list of validation errors that will be displayed at the top of the form.

Usage

add_filter('gform_form_validation_errors', 'your_function_name', 10, 2);

To apply the filter to a specific form, add the form ID after the filter name:

add_filter('gform_form_validation_errors_6', 'your_function_name', 10, 2);

Parameters

  • $errors (array): An array of field validation errors. Each error has keys for field_label, field_selector, and message.
  • $form (Form Object): The current form object.

More information

See Gravity Forms Docs: gform_form_validation_errors

Examples

Add a new error

Add a custom error to the list of validation errors:

add_filter('gform_form_validation_errors', function ($errors, $form) {
    $errors[] = array(
        'field_label'    => 'the field label here',
        'field_selector' => '#field_1_10',
        'message'        => 'the error message here',
    );
    return $errors;
}, 10, 2);

Append phone field instruction

Add a phone format instruction to the error message for phone fields:

add_filter('gform_form_validation_errors', function ($errors, $form) {
    if (empty($errors)) {
        return $errors;
    }
    foreach ($errors as &$error) {
        $selector_parts = explode('_', rgar($error, 'field_selector'));
        $field          = GFAPI::get_field($form, rgar($selector_parts, 2));
        if ($field->get_input_type() !== 'phone') {
            continue;
        }
        $phone_format = $field->get_phone_format();
        if (empty($phone_format['instruction'])) {
            continue;
        }
        $error['message'] .= sprintf(' Phone format: %s', $phone_format['instruction']);
    }
    return $errors;
}, 10, 2);

Placement

This code should be placed in the functions.php file of your active theme or a custom functions plugin.

Since

This filter was added in Gravity Forms v2.5.

Source Code

This filter is located in GFFormDisplay::get_validation_errors() in form_display.php.