Address Validation via Google API and Address Field ONLY

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.

Please help.

Hi Bradley. Have you already gotten the Google API to autocomplete the address information in the form, or is that what you are working on now?

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;
}

Take a look at example 3 on this page:

It covers how to validate all the parts of the address field. This section is key:

        //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' );
        //check to see if the values you care about are filled out
        if ( empty( $street ) || empty( $city ) || empty( $state ) ) {

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.