feat: allow setting dynamic values in remote forms
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 20.8k
- Forks
- 2.3k
- Avg merge
- 1d 16h
- Merged PRs (30d)
- 156
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:
I am open to any solution that avoids the biolerplate.
J
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the remote form API around fields.set, fields.default, and values, then review related issue #15707 and pull request #14815. Define the supported dynamic initialization and reinitialization behavior, including how hydration avoids overwriting user input, and add coverage for the agreed behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- frontend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100