How to Access Updated Latitude and Longitude in beforeSave in Filament?

I want to validate the newly input latitude and longitude in Filament before saving the data to the database, using the beforeSave lifecycle method. However, I'm having trouble ensuring that the updated values entered in the form are available in beforeSave. When I dd the data, the latitude and longitude seem to be missing or reflect old values. How can I ensure that the latest latitude and longitude values from the form are properly sent and accessible in beforeSave? Has anyone encountered a similar issue?
No description
6 Replies
awcodes
awcodes2w ago
There is no way to help you from this. We have no idea what any of these custom fields are doing.
Code A
Code AOP2w ago
Thanks for the reply. Latitude and longitude are updated successfully using Map::make() and afterStateUpdated. However, when I use dd() in beforeSave, the data from address, lat, and lng is not available. Any suggestions on ensuring this data is available in beforeSave?
awcodes
awcodes2w ago
There’s no way to give advice without see the code for the custom fields.
Code A
Code AOP2w ago
Map::make('location') code? The full code looks like this
Map::make('location')
->visible(fn(callable $get, $livewire) => $get('destination_type') === 'umum' && $livewire instanceof \Filament\Resources\Pages\CreateRecord)
->autocomplete('address_custom')
->autocompleteReverse(true)
->defaultLocation([
'lat' => $lat ?? -6.244361430822045,
'lng' => $lng ?? 106.82434694503335,
])
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$set('lat', $state['lat']);
$set('lng', $state['lng']);
if (array_key_exists('address_custom', $state)) {
$set('address_custom', $state['address_custom']);
}
})
Map::make('location')
->visible(fn(callable $get, $livewire) => $get('destination_type') === 'umum' && $livewire instanceof \Filament\Resources\Pages\CreateRecord)
->autocomplete('address_custom')
->autocompleteReverse(true)
->defaultLocation([
'lat' => $lat ?? -6.244361430822045,
'lng' => $lng ?? 106.82434694503335,
])
->reactive()
->afterStateUpdated(function ($state, callable $get, callable $set) {
$set('lat', $state['lat']);
$set('lng', $state['lng']);
if (array_key_exists('address_custom', $state)) {
$set('address_custom', $state['address_custom']);
}
})
and I want to show lat and lng data in beforeSave here
class EditAddress extends EditRecord
{
protected function beforeSave(): void
{
$data = $this->form->getState();
dd($data);
}
....
}
class EditAddress extends EditRecord
{
protected function beforeSave(): void
{
$data = $this->form->getState();
dd($data);
}
....
}
awcodes
awcodes2w ago
Honestly sounds like you’re missing a ->live() somewhere. But what is map, does it have any inputs that would trigger a state change? Seems like you should be setting map from other fields instead of setting map from itself.
josef
josef2w ago
please ask in #google-maps as this pertains to that plugin
Want results from more Discord servers?
Add your server