The gform_form_validation_errors_markup filter allows you to override the markup for the validation errors list, which appears at the top of the form.
Usage
A generic example of using the filter:
add_filter('gform_form_validation_errors_markup', 'your_function_name', 10, 2);
To target a specific form, add the form ID after the filter name:
add_filter('gform_form_validation_errors_markup_6', 'your_function_name', 10, 2);
Parameters
- $validation_errors_markup (string): The HTML for the validation errors list.
- $form (Form Object): The current form object.
More information
See Gravity Forms Docs: gform_form_validation_errors_markup
Examples
Customize error message container
Customize the error message container by adding a CSS class and changing the HTML element:
add_filter('gform_form_validation_errors_markup', 'customize_error_container', 10, 2); function customize_error_container($validation_errors_markup, $form) { // Add a custom CSS class and change the container element to a div $custom_markup = str_replace('<ul', '<div', $validation_errors_markup); $custom_markup = str_replace('</ul>', '</div>', $custom_markup); $custom_markup = str_replace('<li', '<p class="custom-error-class"', $custom_markup); $custom_markup = str_replace('</li>', '</p>', $custom_markup); return $custom_markup; }
Add an icon to error messages
Add an icon before each error message:
add_filter('gform_form_validation_errors_markup', 'add_error_icon', 10, 2); function add_error_icon($validation_errors_markup, $form) { // Add an icon before each error message $custom_markup = str_replace('<li', '<li><i class="fas fa-exclamation-triangle"></i>', $validation_errors_markup); return $custom_markup; }
Remove the error list entirely
Remove the error list markup completely:
add_filter('gform_form_validation_errors_markup', 'remove_error_list', 10, 2); function remove_error_list($validation_errors_markup, $form) { // Return an empty string to remove the error list return ''; }
Wrap error messages in a custom container
Wrap the error messages in a custom container with a specific CSS class:
add_filter('gform_form_validation_errors_markup', 'wrap_error_messages', 10, 2); function wrap_error_messages($validation_errors_markup, $form) { // Wrap the error messages in a custom container $custom_markup = '<div class="custom-error-container">' . $validation_errors_markup . '</div>'; return $custom_markup; }
Add a custom message above the error list
Add a custom message above the error list:
add_filter('gform_form_validation_errors_markup', 'add_custom_error_message', 10, 2); function add_custom_error_message($validation_errors_markup, $form) { // Add a custom message above the error list $custom_message = '<p>Please correct the following errors:</p>'; $custom_markup = $custom_message . $validation_errors_markup; return $custom_markup; }