Hello Gravity Form, I am kind of stuck here how to make this code work. I have a gravity form located on SleepDrops Gift to a Friend - SleepDrops International and I would like to check the two address to not be the same as the other field address just, if it’s possible the Street Address 1 and the Postal Code but if that’s not possible then just the Street Address 1.
Can anyone help me? Thank you.
add_filter( 'gform_field_validation_121_26', 'custom_address_validation', 10, 4 );
function custom_address_validation( $result, $value, $form, $field ) {
//address field will pass $value as an array with each of the elements as an item within the array, the key is the field id
if ( 'address' === $field->type && $field->isRequired ) {
GFCommon::log_debug( __METHOD__ . '(): Running.' );
//address failed validation because of a required item not being filled out
//do custom validation
$street = rgar( $value, $field->id . '.1' );
$street2 = rgar( $value, $field->id . '.2' );
$city = rgar( $value, $field->id . '.3' );
$state = rgar( $value, $field->id . '.4' );
$zip = rgar( $value, $field->id . '.5' );
$country = rgar( $value, $field->id . '.6' );
$master = rgpost( 'input_24_1' );
//check to see if the values you care about are filled out
$required_inputs = array( '1' => $street );
$empty_input = false;
foreach ( $required_inputs as $input_number => $input_value ) {
GFCommon::log_debug( __METHOD__ . '(): Is Hidden? ' . $field->get_input_property( $input_number, 'isHidden' ) );
if ( empty( $input_value ) && ! $field->get_input_property( $input_number, 'isHidden' ) ) {
$field->set_input_validation_state( $input_number, false ); // Only for Gravity Forms 2.5.10 or higher.
$empty_input = true;
GFCommon::log_debug( __METHOD__ . "(): Empty input: {$field->id}.{$input_number}" );
}
}
if ( $empty_input && $result['is_valid'] ) {
$result['is_valid'] = false;
$result['message'] = empty( $field->errorMessage ) ? 'Please enter address.' : $field->errorMessage;
} else if($master == $street ){
$result['is_valid'] = false;
$result['message'] = empty( $field->errorMessage ) ? 'This Address must not be the same as your Address' : $field->errorMessage;
} else {
$result['is_valid'] = true;
$result['message'] = '';
}
}
GFCommon::log_debug( __METHOD__ . '(): Returning validation result.' );
return $result;
}