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 );
/* 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.