Chaloman
Chaloman
FFilament
Created by Chaloman on 4/22/2024 in #❓┊help
afterStateUpdated() Not Triggering in a Custom Filament Field
Thread Title: Issue with afterStateUpdated() Not Triggering in a Custom Filament Field with OpenLayers Body of the Post: Hello everyone, I am working with Filament to develop a form that includes an OpenLayers map. I have a custom field that allows users to select a location on the map, and I need the afterStateUpdated() method to trigger every time the latitude or longitude is updated via a click on the map. Here is the JavaScript snippet:
this.map.on('singleclick', (event) => {
const coords = ol.proj.toLonLat(event.coordinate);
this.$wire.set('latitude', coords[1]);
this.$wire.set('longitude', coords[0]);
});

this.map.on('singleclick', (event) => {
const coords = ol.proj.toLonLat(event.coordinate);
this.$wire.set('latitude', coords[1]);
this.$wire.set('longitude', coords[0]);
});

- Field Configuration in Filament: My field is configured to handle updates to these properties. However, even though the latitude and longitude properties update correctly, the afterStateUpdated() method that I expect to trigger does not. Here is the relevant configuration:
OpenLayers::make('prueba')
->height(350)
->latitude(-50.9032800)
->longitude(-23.1881600)
->afterStateUpdated(function ($state, Forms\Get $get, Forms\Set $set) {
// set the coords here
});

OpenLayers::make('prueba')
->height(350)
->latitude(-50.9032800)
->longitude(-23.1881600)
->afterStateUpdated(function ($state, Forms\Get $get, Forms\Set $set) {
// set the coords here
});

Problem: Despite the latitude and longitude properties updating correctly, the afterStateUpdated() method does not trigger as expected. I would like this method to fire to perform additional logic every time the coordinates change through a map click. Does anyone have experience with this kind of setup or any suggestions on how I might ensure that afterStateUpdated() triggers correctly when updating coordinates? I would appreciate any help or suggestions.
5 replies
FFilament
Created by Chaloman on 7/27/2023 in #❓┊help
Can i use in a form a list checkboxex to show or hide other list of checkboxes
In the user model, I have a list of checkboxes that display roles, and another list of checkboxes that show values from a model called 'instruments'. What I'm trying to do is to have the list of instruments displayed when a certain role is selected, and not displayed otherwise. Some of this works, but when I save it, the values disappear even though they are present in the database. If I refresh the page, they appear as I show you in the following video. Code example
Section::make('Roles')->schema([
CheckboxList::make('roles')
->columnSpan('full')
->reactive()
->relationship('roles', 'name', function (Builder $query) {
if (! auth()->user()->hasRole('super_admin')) {
return $query->where('name', '<>', 'super_admin');
}
return $query;
})
->afterStateUpdated(function (Closure $set, Closure $get) {
$roles = $get('roles');
if ($roles !== null) {
$roleNames = Role::whereIn('id', $roles)->pluck('name')->toArray();
if (! in_array('musico', $roleNames)) {
$set('instruments', null);
}
} else {
$set('instruments', null);
}
})
->getOptionLabelFromRecordUsing(function ($record) {
return Str::of($record->name)->headline();
})->columns(4),
]),
Section::make('Instrumentos')
->when(function (Closure $get): bool {
$roles = $get('roles');
if ($roles !== null) {
$roleNames = Role::whereIn('id', $roles)->pluck('name')->toArray();
return in_array('musico', $roleNames);
}
return false;
})
->schema([
CheckboxList::make('instruments')
->columnSpan('full')
->reactive()
->relationship('instruments', 'name')
->getOptionLabelFromRecordUsing(function ($record) {
return Str::of($record->name)->headline();
})->columns(4),
])
Section::make('Roles')->schema([
CheckboxList::make('roles')
->columnSpan('full')
->reactive()
->relationship('roles', 'name', function (Builder $query) {
if (! auth()->user()->hasRole('super_admin')) {
return $query->where('name', '<>', 'super_admin');
}
return $query;
})
->afterStateUpdated(function (Closure $set, Closure $get) {
$roles = $get('roles');
if ($roles !== null) {
$roleNames = Role::whereIn('id', $roles)->pluck('name')->toArray();
if (! in_array('musico', $roleNames)) {
$set('instruments', null);
}
} else {
$set('instruments', null);
}
})
->getOptionLabelFromRecordUsing(function ($record) {
return Str::of($record->name)->headline();
})->columns(4),
]),
Section::make('Instrumentos')
->when(function (Closure $get): bool {
$roles = $get('roles');
if ($roles !== null) {
$roleNames = Role::whereIn('id', $roles)->pluck('name')->toArray();
return in_array('musico', $roleNames);
}
return false;
})
->schema([
CheckboxList::make('instruments')
->columnSpan('full')
->reactive()
->relationship('instruments', 'name')
->getOptionLabelFromRecordUsing(function ($record) {
return Str::of($record->name)->headline();
})->columns(4),
])
7 replies