How do I refresh select options when I generate new options within the same wizard

Hello, I love using filament so far, everything was kind of well documented to me, but currently I am stuck on the following topic .... - I use a wizard to generate new data based on the data entered in the step before - In a select box I want to display the generated data but the options data is not refreshed, so the users is confused I created some dummy repo as a test case, see here https://github.com/canyoningdb/filament-question1. The major logic is in this file https://github.com/canyoningdb/filament-question1/blob/main/app/Filament/Resources/CanyonResource/Pages/CreateCanyon.php Big thanks for hints and help, Max
5 Replies
toeknee
toeknee2y ago
You need to use a closure on the afterStateUpdated() and $set the field you which to update with the new values
Maxxx22
Maxxx22OP2y ago
Hmm, I tried to using afterStateUpdated before but it seemed not to be working. If I change afterValidation to afterStateUpdated for example even the ID of the select element is updated. How could I set the select options using $set, could you privide an example?
protected function getSteps(): array
{
return [
Step::make('Enter Mandatory Fields')
->schema([
TextInput::make('name')
->default('dummy_name_'.date('his')),
TextInput::make('gps_lat')
->default(random_int(10,80)),
TextInput::make('gps_long')
->default(random_int(10,150)),
])
/*->afterValidation(function (Closure $set, $get) {
$set('region_id', Region::getRegionId($get('gps_lat'), $get('gps_long')));
})*/
->afterStateUpdated(function (Closure $set, $get) {
$set('region_id', Region::getRegionId($get('gps_lat'), $get('gps_long')));
}),
Step::make('Check autogenerated values')
->schema([
Select::make('region_id')
->relationship('region', 'name')
->label('Region (calculated by GPS data)')
->options(Region::all()->pluck('name', 'id')->toArray())
->required(),
]),
];
}
protected function getSteps(): array
{
return [
Step::make('Enter Mandatory Fields')
->schema([
TextInput::make('name')
->default('dummy_name_'.date('his')),
TextInput::make('gps_lat')
->default(random_int(10,80)),
TextInput::make('gps_long')
->default(random_int(10,150)),
])
/*->afterValidation(function (Closure $set, $get) {
$set('region_id', Region::getRegionId($get('gps_lat'), $get('gps_long')));
})*/
->afterStateUpdated(function (Closure $set, $get) {
$set('region_id', Region::getRegionId($get('gps_lat'), $get('gps_long')));
}),
Step::make('Check autogenerated values')
->schema([
Select::make('region_id')
->relationship('region', 'name')
->label('Region (calculated by GPS data)')
->options(Region::all()->pluck('name', 'id')->toArray())
->required(),
]),
];
}
toeknee
toeknee2y ago
Did you set the field to be reactive to receive the afterStateUpdated? $set('my_select', ['My Array of values here]); Will do it But looking at the above, you are trying to handle it on after updating the inputs on reactive
Maxxx22
Maxxx22OP2y ago
I am still not sure how this is working internally but the following example is working
protected function getSteps(): array
{
return [
Step::make('Enter Mandatory Fields')
->schema([
TextInput::make('name')
->default('dummy_name_'.date('his')),
TextInput::make('gps_lat')
->default(random_int(10,80)),
TextInput::make('gps_long')
->default(random_int(10,150)),
])
->afterValidation(function (Closure $set, $get) {
$set('region_id', Region::getRegionId($get('gps_lat'), $get('gps_long')));
})
->afterStateUpdated(function (Closure $set, $get) {
$set('region_id', Region::all()->pluck('name', 'id')->toArray());
}),
Step::make('Check autogenerated values')
->schema([
Select::make('region_id')
->relationship('region', 'name')
->label('Region (calculated by GPS data)')
->reactive()
->required(),
]),
];
}
protected function getSteps(): array
{
return [
Step::make('Enter Mandatory Fields')
->schema([
TextInput::make('name')
->default('dummy_name_'.date('his')),
TextInput::make('gps_lat')
->default(random_int(10,80)),
TextInput::make('gps_long')
->default(random_int(10,150)),
])
->afterValidation(function (Closure $set, $get) {
$set('region_id', Region::getRegionId($get('gps_lat'), $get('gps_long')));
})
->afterStateUpdated(function (Closure $set, $get) {
$set('region_id', Region::all()->pluck('name', 'id')->toArray());
}),
Step::make('Check autogenerated values')
->schema([
Select::make('region_id')
->relationship('region', 'name')
->label('Region (calculated by GPS data)')
->reactive()
->required(),
]),
];
}
Thanks 🙂
toeknee
toeknee2y ago
Ok great
Want results from more Discord servers?
Add your server