SkyMulley
SkyMulley
FFilament
Created by SkyMulley on 8/6/2024 in #❓┊help
Adjusting repeater select box values dynamically
Figured out the solution, I'm not sure if it's the best way to do it but it's the way I've done it
Repeater::make('countries_worked_in')
->schema([
Select::make('continent')
->options(array_map(fn ($continent) => $continent['name'], World::continents()->toArray()))
->required()
->live(),
Select::make('country')
->options(function(Get $get){
if (!$get('continent')) {
return [];
}
$countries = [];
foreach (World::Countries() as $country) {
if ($country->continent_id == $get('continent')+1) {
$countries[$country->name] = $country->name;
}
}
return $countries;
})
->required(),
]),
Repeater::make('countries_worked_in')
->schema([
Select::make('continent')
->options(array_map(fn ($continent) => $continent['name'], World::continents()->toArray()))
->required()
->live(),
Select::make('country')
->options(function(Get $get){
if (!$get('continent')) {
return [];
}
$countries = [];
foreach (World::Countries() as $country) {
if ($country->continent_id == $get('continent')+1) {
$countries[$country->name] = $country->name;
}
}
return $countries;
})
->required(),
]),
Setting a component to live makes the entire form refresh (without losing data). You can then use what I've done in the options field in using a callback with the Get parameter to pull the data from the first field to use in the next after the refresh
4 replies