Require 2 fields match unless the second field is 0 or empty [RESOLVED]

I’m hoping my question is a fairly simple expansion of this post.

https://community.gravityforms.com/t/if-the-number-fields-have-a-comma-gform-field-validation-cant-match-resolved/7720/5

This code is working for me. However, I would like the field being compared (217) to be ignored if it’s 0 or empty. If so, would anyone be willing to share the code to make this possible?

/* Validate that email fields match */
/* Where 2 is the form id and 216 and 217 are the two fields being compared */
    add_filter( 'gform_field_validation_2_216', function ( $result, $value, $form, $field ) {
        $master = rgpost( 'input_217' );
        if ( $result['is_valid'] && $value !== $master ) {
            $result['is_valid'] = false;
            $result['message']  = 'Please verify that your email is entered correctly in both fields.';
        }
     
        return $result;
    }, 10, 4 );

Hello. Can you try adding these three lines?

	if ( $master === 0 || rgempty ( 'input_217', $_POST ) ){
		return $result;
	}

Right after this line:

$master = rgpost( 'input_217' );

So that it looks like this:

/* Validate that email fields match */
/* Where 2 is the form id and 216 and 217 are the two fields being compared */
add_filter( 'gform_field_validation_2_216', function ( $result, $value, $form, $field ) {
	$master = rgpost( 'input_217' );
	// if $master is 0 or $master is empty 
	if ( $master === 0 || rgempty ( 'input_217', $_POST ) ){
		return $result;
	}
	if ( $result['is_valid'] && $value !== $master ) {
		$result['is_valid'] = false;
		$result['message']  = 'Please verify that your email is entered correctly in both fields.';
	}
 
	return $result;
}, 10, 4 );

That should skip this validation when the $master is 0 or $master is empty.

1 Like

Chris,
This worked perfectly. Thanks so much for sharing this! It’s a huge help!
Jeff

1 Like