Repository navigation
feat: allow setting dynamic values in remote forms #16160
Description
Describe the problem
Currently, there is no built-in way to set the initial data for a remote form if the data is dynamic.
If we set the data, it does NOT get updated automatically when the the runes get updated, since setting the initial values themselves are not dynamic.
Note: That matters because .as(type, value) sets the input value, but it does not fully initialize the form field state in the same way as fields.set(...).
This becomes awkward for edit forms, generated forms, shared form components, or any case where the values come from a record rather than being passed field-by-field.
Describe the proposed solution
updateChapterForm.fields.default(() => ({
courseId: initialData.course_id,
chapterId: initialData.id,
title: initialData.title,
}));or
<form
{...updateChapterForm}
values={() => ({
courseId: initialData.course_id,
chapterId: initialData.id,
title: initialData.title,
})}
>Alternatives considered
Currently, I have created a workaround like this:
import type { RemoteForm, RemoteFormInput } from '@sveltejs/kit';
type RemoteFormFields<T extends RemoteFormInput, R = unknown> = Parameters<
RemoteForm<T, R>['fields']['set']
>[0];
export function initForm<T extends RemoteFormInput, R = unknown>(
form: RemoteForm<T, R>,
getter: () => RemoteFormFields<T, R>
) {
let hydrated = false;
const new_value = getter();
form.fields.set(new_value);
$effect(() => {
const values = getter();
if (!hydrated) {
hydrated = true;
return;
}
form.fields.set(values);
});
}and use it like so:
initForm(updateChapterForm, () => ({
courseId: initialData.course_id,
chapterId: initialData.id,
title: initialData.title
}));Obviously, this is not clean and requires boilerplate to work.
Importance
would make my life easier
Additional Information
The important behavior would be:
- Initialize the remote form field state from an object.
- Work with dynamic values from loaded data.
- Avoid requiring every field to pass its value manually through .as(type, value).
- Avoid clobbering user input during hydration.
- Optionally allow reinitialization when a key changes, such as when editing a different record.
Related:
- fields.value() returning undefined when input_value is passed to .as() #15707
- feat: remote form factory #14815
I am open to any solution that avoids the biolerplate.
J
Activity
henrykrinkle01 commentedon Jun 25, 2026
Instead of a new method, is it a good idea to overload
form.fields.set, making it accept either a plain object or a getter?elliott-with-the-longest-name-on-github commentedon Jun 25, 2026
I think the callback would basically just be the same as you wrapping it in an effect, which would also achieve the same result:
henrykrinkle01 commentedon Jun 25, 2026
Not really the same because:
$effectdoesn't work with SSR so you have to set it twice: once outside$effectfor SSR and second time with$effectfor updates, which is what the helper function does. Doesn't feel very svelte$effectdoesn't update values in the same micro tick$effectmay cause bugs with async rendering and fork, at least for now when they are still experimental$effect?jdgamble555 commentedon Jun 25, 2026
@elliott-with-the-longest-name-on-github - No sir. Look at my example above. As @henrykrinkle01 said,
$effectis just one part of it.J
jdgamble555 commentedon Jul 7, 2026
This is related:
#15500