Is there a way to clear fields that have been hidden?
i.e. the user checked a box which shows a nested form, fills it out then changes the check box back to false? I’d like the info they populated in the nested section to be cleared.
[original thread by Steve McKinnon]
Hi @mckinnon(mckinnon), there is no built-in functionality for this use case in JSON Forms so you need to add this yourself. There are two main approaches to solve this problem:
Add a custom renderer for the boolean. It not only changes its “own” data, but also clears the remaining data when unchecked
Listen to data changes outside of JSON Forms and additionally remove the data when the corresponding boolean is set to false
The second approach is a little bit “hacky” as you will also have an intermediate renderer pass in which the boolean is set to false but the data is still there. If this is of no concern then the approach is also fine.
Another approach would be to introduce some middleware, however JSON Forms doesn’t support it out of the box yet. You would need to dig somewhat into the internals to implemement this as a customization for your JSON Forms instance.
Generally speaking I would recommend NOT implementing this
delete behavior. Imagine the user clicking the checkbox by accident and then needing to retype all entered information. This is probably quite frustrating. If you don’t want to send the data to the server you can strip it out beforehand. Of course I don’t know your exact requirements, so the advice might not be applicable.
Thanks @sdirix(sdirix), I agree with you that it would be frustrating for the user to have their data deleted. I was just preempting that someone in my team would ask me this question! Thanks for your detailed response!