gform_validation_message

Description

The gform_validation_message filter is executed when a form fails validation, before the validation message is displayed. Use this filter to change the default validation message.

Usage

Applies to all forms.

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

To limit the scope of your function to a specific form, append the form id to the end of the hook name. (format: gform_validation_message_FORMID)

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

Parameters

ParameterTypeDescription
$messagestringThe validation message to be filtered.
$formForm ObjectThe current form.

Example validation message:

"<div class='validation_error'>" . esc_html__( 'There was a problem with your submission.', 'gravityforms' ) . ' ' . esc_html__( 'Errors have been highlighted below.', 'gravityforms' ) . '</div>'

Examples

Include form title in message.

This example uses the gform_validation_message filter to change the default validation message.

add_filter( 'gform_validation_message', 'change_message', 10, 2 );
function change_message( $message, $form ) {
	return "<div class='validation_error'>Failed Validation - " . $form['title'] . '</div>';
}

Include field validation errors.

add_filter( 'gform_validation_message', function ( $message, $form ) {
	if ( gf_upgrade()->get_submissions_block() ) {
		return $message;
	}

	$message = "<div class='validation_error'><p>There was a problem with your submission. Errors have been highlighted below.</p>";
	$message .= '<ul>';

	foreach ( $form['fields'] as $field ) {
		if ( $field->failed_validation ) {
			$message .= sprintf( '<li>%s - %s</li>', GFCommon::get_label( $field ), $field->validation_message );
		}
	}

	$message .= '</ul></div>';

	return $message;
}, 10, 2 );

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?

Source Code

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