Adjusting repeater select box values dynamically

Is there a way to change the contents of a select box in a repeater based on the user selection on another component. I currently have two select boxes in a repeater, I want to set the second ones options based on the response to the first one.
Solution:
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 ```php Repeater::make('countries_worked_in') ->schema([ Select::make('continent')...
Jump to solution
1 Reply
Solution
SkyMulley
SkyMulley4mo ago
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
Want results from more Discord servers?
Add your server