The gform_validation_message Gravity Forms filter allows you to change the default validation message that is displayed when a form fails validation.
Usage
To apply the filter 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:
add_filter('gform_validation_message_5', 'your_function_name', 10, 2);
Parameters
- $message (string): The validation message to be filtered. Defaults to the standard validation message.
- $form (Form Object): The current form.
More information
See Gravity Forms Docs: gform_validation_message
Source Code: This filter is located in GFFormDisplay::get_form() in form_display.php.
Examples
Include form title in message
Change the default validation message to include the form title:
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
Display a list of fields with their corresponding 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);