Support

Account

Home Forums Feature Requests Consistent Input in Location Field

Solved

Consistent Input in Location Field

  • I’d like to force my users to choose one of the preformatted options that Google provides on the map. Currently, a user can type in something like “san francisco”, hit enter, and that will be a valid input for the field. But I’d like to force that to be “San Francisco, CA, United States”.

    This could be as simple as removing the ability to hit enter when typing text. The user would have to click one of the options in the dropdown. Or it could auto-highlight the first option in the dropdown, and hitting enter would select that.

    I see value in keeping the current functionality, but I think having the option to force predictable inputs would be great. What do you think?

  • To answer my own question, I stumbled on the validate_value filter, which allows me to do this. In my case, I simply wanted the input from the location field to include a city and state in the “San Francisco, CA” format, so I accomplished it like this:

    function google_map_acf_validate_value( $valid, $value, $field, $input ) {
    	if ( ! $valid ) { // bail early if it's already invalid
    		return $valid;
    	}
    	
    	preg_match( '/([^,]+), ([A-Z]{2})\b/', $value['address'], $matches );
    	if ( ! $matches ) { // City was not found
    		$valid = 'Address must include city and state';
    	}
    	
    	return $valid;
    }
    add_filter( 'acf/validate_value/type=google_map', 'google_map_acf_validate_value', 10, 4 );
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Consistent Input in Location Field’ is closed to new replies.