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
Trying to set the default, but $component->getOptions() returns null
If I don't try to GET the value from other fields, this correctly sets the default
8 Replies
Even though custom pages are helpful their state management is itself hustle. Yeah you can experiment if you have time
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?
Have you tried ‘$this-form()->mount()’? This has helped in similar situations.
Where would I call this. I have tried $this->form->fill() but that does not work
Could you share the whole code?
Are you using a statePath?
Line 67 - 90 are where the $get variables are coming from. And line 202 is where they are being used.
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