-
-
Notifications
You must be signed in to change notification settings - Fork 84
Use Svelte 5 Runes for form data #577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
+1 for this, causing sveltejs/svelte#14306 when using superforms + multiselect https://github.com/janosh/svelte-multiselect on svelte 5. |
@trancikk I'm not 100% percent sure what you mean by WA. I'd be interested in your solution though. I've tried wrapping the store in a |
Hello! I will follow up with some kind of solution for now. Save this code snippet in some ts file: import {type FormOptions, superForm, type SuperValidated} from "sveltekit-superforms";
import {fromStore} from "svelte/store";
export function superFormRunes<
T extends Record<string, unknown> = Record<string, unknown>,
M = any,
In extends Record<string, unknown> = T
>(formValidated: SuperValidated<T, M, In> | T, formOptions?: FormOptions<T, M, In>){
// Use it locally.
const formProxy = superForm<T, M, In>(formValidated, formOptions);
// Return unwrapped stores.
return {
...formProxy,
form: fromStore(formProxy.form),
submitting: fromStore(formProxy.submitting),
message: fromStore(formProxy.message),
allErrors: fromStore(formProxy.allErrors),
errors: fromStore(formProxy.errors),
delayed: fromStore(formProxy.delayed),
posted: fromStore(formProxy.posted),
timeout: fromStore(formProxy.timeout),
} satisfies {[key in keyof typeof formProxy]: any}
} What id does:
Usage: let {
data
}: PageProps = $props();
const avatarForm = superFormRunes(data.formAvatar); Why this is much better?
<form enctype="multipart/form-data" method="POST" action="?/avatar" use:avatarForm.enhance>
<input type="text" bind:value={avatarForm.form.current.text}>
</form> |
That should work in many cases, a problem arises though when you want to add options to certain updates, like this one: form.update(
($form) => {
$form.name = "New name";
return $form;
},
{ taint: false }
) |
@dualjack I didn't know Svelte had that @ciscoheat That's a good point. I'll mess around with some possible solutions or my project, and if I stumble across anything good, then I'll post it here. Thanks guys! |
@Tyler-Petrov Me neither. It is not documented at all. Just a reference in API. ![]() |
My first thought was to expose the original, unwrapped objects with names like |
First off I love the library, and it's helped my get a handle of my forms. With that in mind I have a suggestion.
Is your feature request related to a problem? Please describe.
Now that Svelte 5 Runes are a thing I'm not enjoying working with the SuperFormData store. I end up defining the super form, then setting a separate variable for the form data store itself so I can subscribe to it using bind:value on an input. As far as I can tell Svelte doesn't support subscribing to a property of an object. This really isn't a problem if there's only on form on a page, but let's be honest, that rarely happens. Most pages have 2 or more pages, and with a 5 to 10 pages like this things get crazy fast.
This would be a basic form that I would define
Describe the solution you'd like
What I would like to see is a self contained class that can be built and extended with the form data as a Rune, so no $ subscribe is needed. Extending this class would be helpful for a form that is inside of a modal or sheet. The open property could be set of the new class, and keep everything self contained.
I'd like to see an implementation similar to this.
Describe alternatives you've considered
I've tried building a class myself, but I keep running into the stores being an issue to subscribe to. I'm sure there's a somewhat simple way around it, but it seems like the idea I laid out would be helpful for others as well.
The text was updated successfully, but these errors were encountered: