Exclude Hidden Fields from Blocklist Validation

Code

Filename: gpb-exclude-hidden-fields.php

<?php
/**
 * Gravity Perks // Blocklist // Exclude Hidden Fields from Blocklist Validation
 * https://gravitywiz.com/documentation/gravity-forms-blocklist/
 */
add_filter( 'gform_validation', function( $result ) {
	foreach ( $result['form']['fields'] as &$field ) {
		if ( $field->get_input_type() !== 'hidden' ) {
			continue;
		}
		$gpb_validation_message = gf_apply_filters( array( 'gpb_validation_message', $result['form']['id'], $field->id ), __( 'We\'re sorry, the text you entered for this field contains blocked words.', 'gp-blocklist' ) );
		if ( $field->validation_message === $gpb_validation_message ) {
			$field->failed_validation = false;
		}
	}
	$result['is_valid'] = true;
	foreach ( $result['form']['fields'] as $field ) {
		if ( $field->failed_validation ) {
			$result['is_valid'] = true;
		}
	}
	return $result;
}, 11 );

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.