Using Google API to pull Canadian addresses to auto-populate address field only.
Single line address field form.
Trying to use the following code via functions.php file:
add_filter( 'gform_field_validation_13_32', '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 ( $result['is_valid'] ) {
//address failed validation because of a required item not being filled out
//do custom validation
$address = rgar( $value, $field->id . '.1' );
if ( ! empty( $address ) && /** do something else here to determine if the value is a complete address **/ ) {
$result['is_valid'] = false;
$result['message'] = 'This field is required. Please enter at least a street and city.';
} else {
$result['is_valid'] = true;
$result['message'] = '';
}
}
return $result;
}
I need to figure out how I am going to determine if the value contains a complete address.
Yes, Google API is already active and working to autocomplete the address field…
The form is located here if you want to see how we’re doing it: approvalgenie.ca
The issue is, people are failing to complete the full address field, even when presented with the auto-complete suggestion from Google.
I’ve tried a code that Gravity Forms provided, but there needs to be a validation snippet that tells the form that the address field hasn’t been completed with a valid City and/or Province.
Here is the code for the functions.php file:
add_filter( 'gform_field_validation_13_32', '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 ( $result['is_valid'] ) {
//address failed validation because of a required item not being filled out
//do custom validation
$address = rgar( $value, $field->id . '.1' );
if ( ! empty( $address ) && __/** do something else here to determine if the value is a complete address **/__ ) {
$result['is_valid'] = false;
$result['message'] = 'This field is required. Please enter at least a street and city.';
} else {
$result['is_valid'] = true;
$result['message'] = '';
}
}
return $result;
}
Yes, I got that as well from the Gravity Forms support team… but because its’ a single-line field for the Google Address API, the script can’t determine the city/province from that single field input.