Skip to content

feat: Reset form programmatically#14779

Open
sillvva wants to merge 22 commits into
sveltejs:mainfrom
sillvva:form-reset
Open

feat: Reset form programmatically#14779
sillvva wants to merge 22 commits into
sveltejs:mainfrom
sillvva:form-reset

Conversation

@sillvva

@sillvva sillvva commented Oct 21, 2025

Copy link
Copy Markdown
Contributor

fixes #14754
fixes #14210

Implements a form.reset() method that clears the form values, issues, result, submitted, and touched states. In addition, the dev is provided options to not reset certain parts, and the ability to reset the form to new values.

Includes SSR support, so the dev could also use form.reset({ values: { ... } }) in the script block instead of form.fields.set({ ... }) if they wanted to. One advantage is that it allows a DeepPartial<Input> type, instead of requiring the whole object. Unlike .set() this method is only for the base path.

Demo

export type RemoteForm<Input extends RemoteFormInput | void, Output> = {
	...
	/** Reset the form to its initial state */
	reset(options?: {
		/**
		 * Set this to the new values to reset the form to.
		 * Set this to `false` to not reset the values.
		 * @default true
		 */
		values?: DeepPartial<Input> | boolean;
		/** Set this to `false` to not reset the issues. */
		issues?: boolean;
		/** Set this to `false` to not reset the result. */
		result?: boolean;
		/** Set this to `false` to not reset the touched fields. */
		touched?: boolean;
	}): void;
};

It does provide an alternative solution to this issue too:

When navigating to a new page and back on the client, the values (input), issues, and result are not reset. Looking into this, I see that the instance is only deleted on submit if the form has a key from .for(key). This PR would give developers the ability to reset without using .for().


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot

changeset-bot Bot commented Oct 21, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 314c665

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot

Copy link
Copy Markdown

@sillvva sillvva changed the title Form reset Reset form programmatically Oct 21, 2025
@sillvva sillvva changed the title Reset form programmatically feat: Reset form programmatically Oct 21, 2025
@sillvva sillvva mentioned this pull request Oct 26, 2025
6 tasks
@Sourabhpatel1

This comment was marked as duplicate.

@gauel

This comment was marked as duplicate.

@teemingc teemingc added the forms Stuff relating to forms and form actions label Jan 6, 2026
Comment on lines +2106 to +2107
* Set this to the new values to reset the form to.
* Set this to `false` to not reset the values.

@teemingc teemingc Feb 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Set this to the new values to reset the form to.
* Set this to `false` to not reset the values.
* Resets the form's values if `true` or sets new ones if an object is passed instead .

* @default true
*/
values?: DeepPartial<Input> | boolean;
/** Set this to `false` to not reset the issues. */

@teemingc teemingc Feb 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** Set this to `false` to not reset the issues. */
/**
* Resets the form's issues if `true`.
* @default true
*/

values?: DeepPartial<Input> | boolean;
/** Set this to `false` to not reset the issues. */
issues?: boolean;
/** Set this to `false` to not reset the result. */

@teemingc teemingc Feb 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** Set this to `false` to not reset the result. */
/**
* Resets the form's submission results if `true`.
* @default true
*/

issues?: boolean;
/** Set this to `false` to not reset the result. */
result?: boolean;
/** Set this to `false` to not reset the touched fields. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/** Set this to `false` to not reset the touched fields. */
/**
* Resets each form field's touched state if `true`.
* @default true
*/

submit().then(() => {
if (!issues.$) {
form.reset();
instance.reset({ result: false });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
instance.reset({ result: false });
instance.reset();

I think we should continue to reset the values after a successful submission as before

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, we should be able to configure that. For example, in a search form: you do not want the search query to be cleared or to need to repopulate it with the previous value after submission.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I figured something like https://svelte.dev/docs/kit/form-actions#GET-vs-POST would be good enough where you'd use a query + GET action form but maybe it's not? (you'd lose all the niceties of the form remote function such as validation if needed)

@@ -0,0 +1,20 @@
<script lang="ts">
import { test } from './form.remote.js';
test.reset({ values: { value: 'hi' } });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test.reset({ values: { value: 'hi' } });
test.fields.set({ value: 'bye' });
test.reset({ values: { value: 'hi' } });

Wonder if it's worth adding a .set here to help check that it resets those too

@teemingc teemingc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I left a few small comments. Other than that, this should be good to merge.

Comment thread packages/kit/src/exports/public.d.ts Outdated
- `touched` — Set to `false` to preserve touched field tracking (default is `true`)

```svelte
<button onclick={() => login.reset({ values: { username: 'guest', password: 'guest' } })}>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing something but this API feels a bit complicated to me — wouldn't it be better to do this?

<button onclick={() => {
  login.reset();
  login.fields.set({ username: 'guest', password: 'guest' });
}>
  Login as guest
</button>

(In fact in this example I'm not sure I'd bother with login.reset() at all)

@sillvva sillvva Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that wasn't a good example to use.

An instance where this would be used is when you want to reset the form to some default values after submission when the form doesn't redirect.

Also, sometimes when navigating to another page and back, the form does not reset (something the form factory PR was also intended to address). The idea was that if this behavior was intentional, then you could use the .reset() API to clear the form when navigating away from or to the page.

The values option probably could have been simplified to only clear the values. I added the set feature built-in as a nice-to-have option as well.

@chillingpulsar

This comment was marked as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

forms Stuff relating to forms and form actions

Projects

None yet

8 participants