sveltekit-superforms
sveltekit-superforms copied to clipboard
Form resets dirty values when an invalidate forces load functions belonging to the currently active page to re-run
Description
I have a very simple superform and an unrelated background process that it causes an invalidateAll to be called occasionally. If I have dirty values in the superform when the invalidateAll happens it causes the pages load function to be called again and then wipes out all dirty values from the form even though the values the server returns for the form are the initial values. I think the form should allow the dirty values to persist.
If applicable, a MRE See repro here: https://www.sveltelab.dev/508vyoj6r8j2938
I just took the template and added a setInterval that runs an invalidateAll every 5 seconds. To repro, just try to type some text in the name field and watch it vanish.
This is a very special case, since you return superValidate data in the load function, which should re-instantiate the form. It's basically a page reload (as will happen without JS enabled).
If you had more data returned from the load function, how would you determine which one should change or not?
return {
form: await superValidate(zod(schema)),
random: Math.random() // Should this one stay intact as well? How to know?
}
What I can do is to add an applyAction: 'never' option that will prevent any page updates from modifying the form. It'll only work with use:enhance enabled, of course.
If it was readonly data I would refresh with the latest from the server. But if I have a form that a user is working on, I would like those dirty values not to get wiped. But I think you are right that there is not a universal right answer here and your proposed solution seems reasonable.
Great, I'll make a new release soon!
Will this also ensure errors don't get cleared too?
Yes, nothing except submitting the form and modifying it explicitly will change its state.
Added now in 2.25.0