How to save the value of hidden toggle?

I have two toggles.

If the first one is on, it will show the second toggle.

If the frist one is off, it will hide the second toggle.

I am using this on the first toggle to set the value to off when offing the first toggle:

->afterStateUpdated(function (Set $set, $state) {
    if ($state === false) {
        $set('second_thing', false);
    }
})

The problem is that while it seems to be working in the form visually, it is not saving the state of the second toggle (second_thing) in the DB, just the first toggle is saved correctly. As if the afterStateUpdated doesn't propagate to the save method?

Because if I manually (click with the mouse) on the second toggle directly to the state I want and press save - it is saved correctly.

How to alter the afterstateupdated method (or use something else) to save it correctly?
Was this page helpful?