Skip to content

feat: allow setting dynamic values in remote forms #16160

Description

@jdgamble555

Describe the problem

Currently, there is no built-in way to set the initial data for a remote form if the data is dynamic.

Image

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:

  1. Initialize the remote form field state from an object.
  2. Work with dynamic values from loaded data.
  3. Avoid requiring every field to pass its value manually through .as(type, value).
  4. Avoid clobbering user input during hydration.
  5. 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

Activity

  1. henrykrinkle01 commented on Jun 25, 2026

    @henrykrinkle01
    Contributor

    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?

    updateChapterForm.fields.set(() => ({
      courseId: initialData.course_id,
      chapterId: initialData.id,
      title: initialData.title,
    }));
  2. elliott-with-the-longest-name-on-github commented on Jun 25, 2026

    @elliott-with-the-longest-name-on-github
    Contributor

    I think the callback would basically just be the same as you wrapping it in an effect, which would also achieve the same result:

     $effect(() => form.fields.set(...));
  3. henrykrinkle01 commented on Jun 25, 2026

    @henrykrinkle01
    Contributor

    Not really the same because:

    1. $effect doesn't work with SSR so you have to set it twice: once outside $effect for SSR and second time with $effect for updates, which is what the helper function does. Doesn't feel very svelte
    2. $effect doesn't update values in the same micro tick
    3. $effect may cause bugs with async rendering and fork, at least for now when they are still experimental
    4. Are we really promoting setting a state inside $effect?
  4. jdgamble555 commented on Jun 25, 2026

    @jdgamble555
    Author

    @elliott-with-the-longest-name-on-github - No sir. Look at my example above. As @henrykrinkle01 said, $effect is just one part of it.

    J

  5. jdgamble555 commented on Jul 7, 2026

    @jdgamble555
    Author

    This is related:

    #15500

  6. added
    formsStuff relating to forms and form actions
    and removed on Aug 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    formsStuff relating to forms and form actions

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions