Top Level Validation Message for Hidden Fields

If a Limit Submissions feed returns a validation error and is based on the field values of a hidden field, the feed’s Limit Message will be used as the form’s top level validation message.

Code

Filename: gpls-top-level-validation-mesage-for-hidden-fields.php

<?php
/**
 * Gravity Perks // GP Limit Submissions // Top Level Validation Message for Hidden Fields
 * https://gravitywiz.com/documentation/gravity-forms-limit-submissions/
 *
 * If a Limit Submissions feed returns a validation error and is based on the field values of a hidden field, the
 * feed's Limit Message will be used as the form's top level validation message.
 */
add_filter( 'gform_validation_message', function ( $message, $form ) {

	$has_other_error  = false;
	$gpls_error_field = false;

	foreach ( $form['fields'] as $field ) {

		if ( ! $field->failed_validation ) {
			continue;
		}

		if ( ( $field->visibility === 'hidden' || $field->get_input_type() === 'hidden' ) && strpos( $field->validation_message, 'gpls-limit-message' ) ) {
			$gpls_error_field = $field;
		} else {
			$has_other_error = true;
		}
	}

	if ( $gpls_error_field && ! $has_other_error ) {
		$message = sprintf( "<div class='validation_error'>%s</div>", $gpls_error_field->validation_message );
	}

	return $message;
}, 10, 2 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.