default value when hidden()

I created a field that is only visible when a toggle is set (using visible()). This works fine, but when I update the field value AND toggle the field mentioned, the value of this field is not saved. I understand why this is the case: the field is not included in the post-data because it's not visible. Is there a way to set a "default"-value when the field is not visible? dehydrateStateUsing() does not seem to work (even with dehydratedWhenHidden()? I know I could use the lifecycle-hooks, or even model-events but I want to include it in the form (to keep it all together). Any ideas?
8 Replies
Tetracyclic
Tetracyclic7mo ago
If you want it directly in the form, you could use a hidden field to store the actual value, with a default set. And update the state of the hidden field when state if your visible field changes.
pinoooh
pinooohOP7mo ago
an extra (hidden) field... that's clever. Is there no way to achieve the same result but with methods on the original (hidden/visible) field?
Tetracyclic
Tetracyclic7mo ago
dehydratedWhenHidden() seems like it was added to fix an issue in MorphToSelect, though I'm not clear why it wouldn't be working for this Is the field inside any kind of container that's also being hidden?
pinoooh
pinooohOP7mo ago
no, it's right next to the toggle
Tetracyclic
Tetracyclic7mo ago
You probably would need to mutate the data on save instead, if the field is hidden.
pinoooh
pinooohOP7mo ago
Looking at this now, and altering the state in the beforeSave looks slightly better than using an additional field I guess... thanks for thinking along though!
Tetracyclic
Tetracyclic7mo ago
Hopefully one of the core team can shed light on why dehydrateWhenHidden() doesn't work in this instance.
Adam Holmes
Adam Holmes7mo ago
+1 for dehydratedWhenHidden() - helped me out a bunch
Want results from more Discord servers?
Add your server