Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline
A lightning:inputAddress component creates a compound field that includes the following constituent fields.
Street
City
Province
Country
Postal code
The street field is displayed as a multi-line text field. The other fields are displayed as a text input field by default. The country and province fields are displayed as dropdown menus if you specify countryOptions and provinceOptions to provide options for menu items.
This example creates an address compound field with attributes to specify values for the constituent fields. The initial values are set directly with the attributes.
To create a dropdown menu for the country and province, pass in an array of label-value pairs to countryOptions and provinceOptions. Use the country and province attributes to specify the default values on the dropdown menus.
This example adds a custom attribute previousCountry that’s used to check if the country is changed.
The client-side controller and helper Javascript file initialize the picklist values. The updateProvinces function is called when you change any address field.
The province options update only when you select a different country. The custom attribute previousCountry value is compared to the value of country before updating provinceOptions. This improves performance. Without the comparison, the provinceOptions picklist is always updated when you change a field in the address, even if you don’t change the country selection.
Alternatively, you can enable state and country picklists in your org, and access the values through an Apex controller. For more information, see Let Users Select State and Country from Picklists in Salesforce Help.
Using the Locale Information
In Lightning Experience, the locale value corresponds to the Locale field on the Language & Time Zone page in the user’s settings. For example, if you select “French (France)” in the Locale field, lightning:inputAddress uses fr-FR as the locale.
To override the locale on your user’s settings, provide your own locale value.
In this example, the {!$Locale.userLocaleLang} global value provider returns fr and the {!$Locale.userLocaleCountry} global value provider returns FR. The lightning:inputAddress component uses the en-US locale.
For more information, see $Locale in the Lightning Aura Components Developer Guide.
If you pass in an invalid locale, the component uses en-US. The locale supports both hyphens and underscores, for example, en-US or en_US.
Hiding the Province Field
The province field is used with countries that include a province in their addresses. This field can also be used as a field for state information, such as in United States addresses.
To visually hide the province field from the component’s fields layout for locales that don’t require it, set the hideProvince attribute to true. For example, you can use hideProvince when the locale is fr-FR or zh-CN.
If you don’t provide a value for provinceLabel, the component renders with a province field without a label. Hide the field using hideProvince="true".
Using Lookup to Find and Autofill an Address
To enable autocompletion of the address fields using an address lookup field, include the showAddressLookup attribute. The address lookup field is placed above the address fields you provide.
When you start typing an address in the lookup field, a dropdown appears with matching addresses returned by the Google Maps Places API. Select an address from the dropdown to populate the address fields.
When location services is enabled on your browser, the component uses the browser’s Geolocation API to determine the initial location. A timeout can occur due to factors such as browser security settings, network conditions, and device capabilities. If the Geolocation API times out after 10 seconds, the address lookup field defaults to San Francisco, CA with a latitude of 37.790091 and longitude of -122.396848 instead of your current position. To enable location services on your browser, see Autocomplete Addresses in Salesforce Help.
Filter Address Lookup Based on Country Options
To filter address lookups by country, pass in an array of ISO 3166-1 Alpha-2 country code strings to the countryLookupFilter attribute. Country codes are case-insensitive. You can specify a maximum of five country codes. Enable address lookup by including showAddressLookup.
If you pass in an invalid country code or more than five codes, Google Maps Places API produces console errors and returns no results.
Using Compact Address Fields
By default, the Street address field is a textarea field. Use compact address fields if you want to separate the Street address field into two input fields instead.
To display compact address fields, include the showCompactAddress attribute. Use streetLabel to provide a label for the first line of address, and use subpremiseLabel for the second line of address. You can add supplementary information on this field, such as a building number or unit name.
1<lightning:inputAddress2 showCompactAddress="true"3 addressLabel="Address"4 streetLabel="Street Line 1"5 subpremiseLabel="Street Line 2"6 cityLabel="City"7 countryLabel="Country"8 provinceLabel="Province"9 postalCodeLabel="Postal Code"10></lightning:inputAddress>
Using the Compact Address Fields with Address Lookup
To enable compact address fields with address lookup, include the showAddressLookup and showCompactAddress attributes. These attributes enable the first street address field as a lookup field.
When you start typing an address in the lookup field, a dropdown menu displays matching addresses returned by the Google Maps Places API. Select an address from the dropdown to populate the address fields.
Using the Subpremise Field with Address Lookup
Subpremise information includes apartment, unit, or floor number. To provide a label on the subpremise field, use the subpremiseLabel attribute. The second address field is used as the subpremise field.
You can provide a subpremise value using the subpremise attribute. The first input field continues to use streetLabel. The subpremise field doesn’t require a value for submission.
1<lightning:inputAddress2 showAddressLookup="true"3 showCompactAddress="true"4 addressLabel="Address"5 streetLabel="Street Line 1"6 subpremiseLabel="Street Line 2"7 cityLabel="City"8 countryLabel="Country"9 provinceLabel="Province"10 postalCodeLabel="Postal Code"11></lightning:inputAddress>
Google Maps API currently supports subpremises for Australia, New Zealand, and Canada only. The placement of subpremise information is different for each country. For example, Australian addresses include the subpremise information as a prefix to street number, such as 2/1 Tully Road where 2 is the subpremise. When you enter this address and select it from the search results, the street field is populated with the subpremise and street information while the subpremise field remains empty.
Populating the subpremise field using address lookup isn’t currently supported. For example, US addresses include the subpremise after the street number, such as 123 Main St, Unit 10. When you enter the address with a subpremise, the subpremise appears with the street address in the result list. However, selecting the address from the search result doesn’t populate the subpremise field. The street field also doesn’t preserve the subpremise, but you can enter it manually in the street or subpremise field.
Validating Required Fields
When you set required="true", a red asterisk is displayed on every address field to indicate that an entry in each field is required. An error message is displayed below a field if a user interacted with it and left it blank. The required attribute is not enforced and you must validate it before submitting a form that contains an address compound field.
Let’s say you have a lightning:button component that calls the handleClick controller action. You can display the error message when a user clicks the button without providing a value on a field.
1({2 handleClick: function(cmp, event){3 var address = cmp.find("myaddress");4 var isValid = address.checkValidity();5 if(isValid){6 alert("Creating new address");7}else{8 address.showHelpMessageIfInvalid();9}10},11});
Working with Labels and Placeholders
A label is associated with an address field and it enables screen readers to navigate the form correctly. Include a label for each field you’re using, with the following attributes.
addressLabel
streetLabel
cityLabel
provinceLabel
countryLabel
postalCodeLabel
You can hide the addressLabel visually and still make them accessible to screen readers by using variant="label-hidden".
Additionally, the showAddressLookup boolean attribute creates a search field that doesn’t have an associated label. See Using Lookup to Find and Autofill an Address for more information.
Your Salesforce locale setting determines the order and layout of the address fields by default. Use the locale attribute to override the default. Specify any locale code from the list of Supported Number, Name, and Address Formats (ICU) .
We recommend that you provide a label even when you provide placeholder text for an address field. Without field labels, users can lose context when the placeholder text disappears as they type in the field.
Specify placeholder text to give users a hint about the content they’re expected to enter in the field. Avoid repeating the field label in the placeholder for better accessibility. Consider the width of your address field as the placeholder text is cut off if it’s too long, especially on mobile devices.
Include an optional placeholder for each field you’re using, with the following attributes.
addressLookupPlaceholder
streetPlaceholder
cityPlaceholder
provincePlaceholder
countryPlaceholder
postalCodePlaceholder
Usage Considerations
Using showAddressLookup is not supported in Experience Builder sites, Lightning Out, Lightning Components for Visualforce, and standalone apps.
When working with address fields such as with the MailingAddress field on Salesforce records, consider using the record form components. The lightning:recordForm, lightning:recordViewForm, and lightning:recordEditForm components provide a form-based UI that’s metadata-driven. The components are automatically wired up to your record data, labels, and field-level help text. For more information, see Lightning Data Service.
To create your own custom UI to work with Salesforce records, use lightning:inputAddress with the force:recordData component.
The placeholder for the address lookup field option. Visible only when using showAddressLookup.
String
body
The body of the component. In markup, this is everything in the body of the tag.
Aura.Component[]
city
The value for the city field. Maximum length is 40 characters.
String
cityLabel
The label for the city field.
String
cityPlaceholder
The placeholder for the city field.
String
class
A CSS class for the outer element, in addition to the component's base classes.
String
country
The value for the country field. If countryOptions is provided, this country value is selected by default. Maximum length is 80 characters.
String
countryDisabled
Specifies whether the country field is disabled. This value defaults to false.
Boolean
countryLabel
The label for the country field.
String
countryLookupFilter
A list of ISO 3166-1 Alpha-2 country codes to filter the address with. Country codes are case-insensitive. Use with the showAddressLookup attribute. Specify up to five country codes.
List
countryOptions
The array of label-value pairs for the country. Displays a dropdown menu of options.
List
countryPlaceholder
The placeholder for the country field.
String
disabled
Specifies whether the address fields are disabled. This value defaults to false.
Boolean
fieldLevelHelp
Help text detailing the purpose and function of the address compound field.
String
hideProvince
Specifies whether the province field (only if optional) is hidden. This value defaults to false.
Boolean
locale
Specifies the locale used to determine the layout of the address fields. This value defaults to en-US.
String
onblur
The action triggered when the input releases focus.
Aura.Action
onchange
The action triggered when the value changes.
Aura.Action
onfocus
The action triggered when the input receives focus.
Aura.Action
postalCode
The value for the postal code field. Maximum length is 20 characters.
String
postalCodeLabel
The label for the postal code field.
String
postalCodePlaceholder
The placeholder for the postal code field.
String
province
The value for the province field. If provinceOptions is provided, this province value is selected by default. Maximum length is 80 characters.
String
provinceLabel
The label for the province field.
String
provinceOptions
The array of label-value pairs for the province. Displays a dropdown menu of options.
List
provincePlaceholder
The placeholder for the province field.
String
readonly
Specifies whether the address fields are read-only. This value defaults to false.
Boolean
required
Specifies whether the address fields are required. This value defaults to false.
Boolean
showAddressLookup
Specifies whether to enable address lookup using Google Maps. This value defaults to false.
Boolean
showCompactAddress
Specifies whether to enable compact address fields, which renders the street field as two separate inputs instead of a single text area. To provide a label for the first street field, use streetLabel. To provide a label for the second street field, use subpremiseLabel.
Boolean
street
The value for the street field. Maximum length is 255 characters when rendered as a textarea. Maximum length is 80 characters when rendered as an input using showCompactAddress.
String
streetLabel
The label for the street field.
String
streetPlaceholder
The placeholder for the street field.
String
subpremise
The value for the subpremise field. Maximum length is 80 characters. Use this attribute with showCompactAddress.
String
subpremiseLabel
The label for the subpremise field. Use this attribute with showCompactAddress.
String
subpremisePlaceholder
The placeholder for the subpremise field. Use this attribute with showCompactAddress.
String
title
Displays tooltip text when the mouse moves over the element.
String
variant
The variant changes the appearance of the address compound field. Accepted variants include standard, label-inline, label-hidden, and label-stacked. This value defaults to standard. Use label-hidden to hide the compound field label but make it available to assistive technology. Use label-inline to horizontally align the label and address fields. Use label-stacked to place the label above the address fields.
String
Methods
Name
Description
Argument Name
Argument Type
Argument Description
blur
Removes focus from the element.
checkValidity
Returns the valid property value (Boolean) on the ValidityState object to indicate whether the compound field has any validity errors.
focus
Sets focus on the element.
reportValidity
Display error messages if the compound field is invalid.
setCustomValidityForField
Sets a custom error message to be displayed for the address fields when the field values are submitted.
message
String
The string that describes the error. If message is an empty string, the error message is reset.
fieldName
String
The name of the address field, which must be one of the following: street, city, province, postalCode, country.
showHelpMessageIfInvalid
Shows the help message if the compound field is in an invalid state.