-
Notifications
You must be signed in to change notification settings - Fork 2
GoogleAddress
Pair\Html\FormControls\GoogleAddress is Pair's Google-enhanced address form control.
It keeps the visible field as a normal address input, but it can also persist Google-specific metadata in hidden fields:
place_id- latitude
- longitude
- JSON-encoded address components
This makes it useful when you want a classic Pair form that still stores structured location data.
Use GoogleAddress when:
- users should type a normal address, but with Google autocomplete assistance
- the project wants to save a stable
place_idfor later server-side lookups - you also need coordinates or structured components for mapping, distance, or validation logic
Do not use it when you only need a plain text address field. In that case, Address is enough.
- It extends Address, so the main field stays compatible with normal Pair forms.
- It adds the
pairGoogleAddressCSS class and a set ofdata-*attributes consumed by/assets/PairGoogleMaps.js. - The browser-side enhancement is opt-in and requires
GoogleMaps::load(). - Pair clears bound metadata fields if the user manually edits the visible text after a previous Google selection.
Configures a hidden field that stores the selected Google place_id.
use Pair\Helpers\GoogleMaps;
use Pair\Html\Form;
GoogleMaps::load();
$form = new Form();
// Keep the human-readable address plus the Google place ID.
$form->googleAddress('shippingAddress')
->label('Shipping address')
->placeIdField('shippingAddressPlaceId');coordinatesFields(string $latitudeName, string $longitudeName, float|string|null $latitudeValue = null, float|string|null $longitudeValue = null): static
Configures hidden latitude and longitude inputs.
This is one of the most common additions because it lets the application geocode once in the browser and reuse the coordinates later.
use Pair\Helpers\GoogleMaps;
use Pair\Html\Form;
GoogleMaps::load();
$form = new Form();
// Save coordinates together with the visible address.
$form->googleAddress('serviceAddress')
->label('Service address')
->coordinatesFields('serviceLatitude', 'serviceLongitude');Configures a hidden field that stores the normalized Google address components as JSON.
If you pass an array as the initial value, Pair JSON-encodes it automatically.
use Pair\Helpers\GoogleMaps;
use Pair\Html\Form;
GoogleMaps::load();
$form = new Form();
// Persist the structured Google components for later parsing.
$form->googleAddress('billingAddress')
->label('Billing address')
->componentsField('billingAddressComponents');Restricts autocomplete suggestions to one or more ISO 3166-1 alpha-2 country codes.
Restricts suggestions to specific Google place types.
Biases autocomplete toward a rectangular viewport.
When enabled, Google should only return in-bounds results.
These four methods are usually chained together:
use Pair\Helpers\GoogleMaps;
use Pair\Html\Form;
GoogleMaps::load();
$form = new Form();
// Restrict the field to Italian addresses inside the Milan area.
$form->googleAddress('officeAddress')
->label('Office address')
->countries(['it'])
->bounds(45.4300, 9.1200, 45.5000, 9.2500)
->strictBounds()
->types(['address']);Renders the visible address input plus the configured hidden metadata inputs.
Most code does not call render() directly because Pair forms normally render controls through Form, but it is the method that assembles:
- the visible
Addressfield - the
pairGoogleAddressclass - the Google-specific
data-*attributes - the optional hidden inputs for
place_id, coordinates, and components
use Pair\Helpers\GoogleMaps;
use Pair\Html\Form;
GoogleMaps::load();
$form = new Form();
// Build one field that stores both the visible address and Google metadata.
$form->googleAddress('shippingAddress')
->label('Shipping address')
->placeIdField('shippingAddressPlaceId')
->coordinatesFields('shippingLatitude', 'shippingLongitude')
->componentsField('shippingAddressComponents')
->countries(['it'])
->required();
echo $form->renderControls();Editing an existing record with prefilled metadata:
use Pair\Helpers\GoogleMaps;
use Pair\Html\Form;
GoogleMaps::load();
$form = new Form();
// Rehydrate previously saved values when editing a record.
$form->googleAddress('serviceAddress')
->label('Service address')
->value($provider->serviceAddress)
->placeIdField('serviceAddressPlaceId', $provider->serviceAddressPlaceId)
->coordinatesFields('serviceLatitude', 'serviceLongitude', $provider->serviceLatitude, $provider->serviceLongitude)
->componentsField('serviceAddressComponents', $provider->serviceAddressComponents);Reacting to the browser-side selection event:
document.addEventListener('pair:google-address-selected', (event) => {
// Use the normalized payload emitted by PairGoogleMaps.js.
console.log(event.detail.placeId, event.detail.latitude, event.detail.longitude);
});Internally, GoogleAddress renders hidden inputs only for the fields you explicitly configured. If you never call placeIdField(), coordinatesFields(), or componentsField(), no extra hidden inputs are added.
The control also writes the dataset consumed by /assets/PairGoogleMaps.js, including:
data-google-place-id-fielddata-google-latitude-fielddata-google-longitude-fielddata-google-components-fielddata-google-countriesdata-google-typesdata-google-boundsdata-google-strict-bounds
-
Form::googleAddress()is the usual factory method used in Pair projects. - The browser-side helper looks up bound fields by exact HTML
name. - Manual edits after a selection clear the hidden metadata fields to avoid stale
place_idor coordinates.
See also: Form, FormControl, Address, GoogleMaps, GoogleGeocoder, GooglePlacesService.