gform_form_validation_errors

Description

The gform_form_validation_errors filter enables the list of validation errors, which will be displayed at the top of the form, to be overridden.

Usage

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

You can also specify this per form by adding 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. The following keys will be defined for each error: field_label, field_selector, and message.

  • $form Form Object

    The current form object.

Examples

Add a new error

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_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.