F
Filament6mo ago
Kitty

Problem with afterStateHydrated

In a custom page, I am trying to set my toggle buttons default value based on its dynamic options. The options are based on different field values. I have tried both default() and afterStateHydrated(). Even though I can populate the options correctly, I can not access the fields options in either of those methods when the options are dependent on other fields. How can I fix this? Assume: $get('dob') = '02/20/2022' and $get('prematurity_id') = '02/30/2022' This correctly gets the options
ToggleButton::make('questionnaire_id')
->options( fn (Get $get) =>
self::getQuestionnaireOptions($get('dob'), $get('prematurity_id'))
)
->inline()
->label(trans('Select Questionnaire'))
->required(),
ToggleButton::make('questionnaire_id')
->options( fn (Get $get) =>
self::getQuestionnaireOptions($get('dob'), $get('prematurity_id'))
)
->inline()
->label(trans('Select Questionnaire'))
->required(),
Trying to set the default, but $component->getOptions() returns null
ToggleButton::make('questionnaire_id')
->options( fn (Get $get) =>
self::getQuestionnaireOptions($get('dob'), $get('prematurity_id'))
)
->afterStateHydrated(function (ToggleButton $component) {
$component->state(array_key_first($component->getOptions()));
})
->inline()
->label(trans('Select Questionnaire'))
->required(),
ToggleButton::make('questionnaire_id')
->options( fn (Get $get) =>
self::getQuestionnaireOptions($get('dob'), $get('prematurity_id'))
)
->afterStateHydrated(function (ToggleButton $component) {
$component->state(array_key_first($component->getOptions()));
})
->inline()
->label(trans('Select Questionnaire'))
->required(),
If I don't try to GET the value from other fields, this correctly sets the default
ToggleButton::make('questionnaire_id')
->options( fn (Get $get) =>
self::getQuestionnaireOptions('02/20/2022', '02/30/2022')
)
->afterStateHydrated(function (ToggleButton $component) {
$component->state(array_key_first($component->getOptions()));
})
->inline()
->label(trans('Select Questionnaire'))
->required(),
ToggleButton::make('questionnaire_id')
->options( fn (Get $get) =>
self::getQuestionnaireOptions('02/20/2022', '02/30/2022')
)
->afterStateHydrated(function (ToggleButton $component) {
$component->state(array_key_first($component->getOptions()));
})
->inline()
->label(trans('Select Questionnaire'))
->required(),
8 Replies
Sangram_11
Sangram_116mo ago
Even though custom pages are helpful their state management is itself hustle. Yeah you can experiment if you have time
Kitty
KittyOP6mo ago
I got time and hustle I am thinking the problem is that when afterStateHydrated is called the fields $get is pulling from are empty. It's not until a user interacts with those fields that the toggle button options are updated. So I need something that reruns the hydration?
Chrispian
Chrispian6mo ago
Have you tried ‘$this-form()->mount()’? This has helped in similar situations.
Kitty
KittyOP6mo ago
Where would I call this. I have tried $this->form->fill() but that does not work
LeandroFerreira
LeandroFerreira6mo ago
Could you share the whole code? Are you using a statePath?
Kitty
KittyOP6mo ago
Gist
gist:ae1f384aceddbd83ebe78b33395c2886
GitHub Gist: instantly share code, notes, and snippets.
Kitty
KittyOP6mo ago
Line 67 - 90 are where the $get variables are coming from. And line 202 is where they are being used.
Kitty
KittyOP6mo ago
What I have debugged so far is that when the toggle buttons are filled the fields it is dependent on are empty so $component->getOptions() is empty. I have been trying various afterStateUpdated calls on the fields, and it almost works, but I feel like I am chasing the cats tails. You can see that in https://gist.github.com/griggsk/ae1f384aceddbd83ebe78b33395c2886#file-gistfile2-txt
Gist
gist:ae1f384aceddbd83ebe78b33395c2886
GitHub Gist: instantly share code, notes, and snippets.
Want results from more Discord servers?
Add your server