feat: Reset form programmatically#14779
Conversation
🦋 Changeset detectedLatest commit: 314c665 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
This comment was marked as duplicate.
| * Set this to the new values to reset the form to. | ||
| * Set this to `false` to not reset the values. |
There was a problem hiding this comment.
| * 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. */ |
There was a problem hiding this comment.
| /** 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. */ |
There was a problem hiding this comment.
| /** 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. */ |
There was a problem hiding this comment.
| /** 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 }); |
There was a problem hiding this comment.
| instance.reset({ result: false }); | |
| instance.reset(); |
I think we should continue to reset the values after a successful submission as before
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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' } }); | |||
There was a problem hiding this comment.
| 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
left a comment
There was a problem hiding this comment.
Thank you! I left a few small comments. Other than that, this should be good to merge.
| - `touched` — Set to `false` to preserve touched field tracking (default is `true`) | ||
|
|
||
| ```svelte | ||
| <button onclick={() => login.reset({ values: { username: 'guest', password: 'guest' } })}> |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
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 ofform.fields.set({ ... })if they wanted to. One advantage is that it allows aDeepPartial<Input>type, instead of requiring the whole object. Unlike.set()this method is only for the base path.Demo
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, andresultare not reset. Looking into this, I see that the instance is only deleted on submit if the form has akeyfrom.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:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.Edits