Skip to content

Commit e317457

Browse files
committed
Added force invalidateAll option.
1 parent 71feddf commit e317457

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Type narrowing for `FormPath` and its relatives, to filter the paths based on a specific type, like `FormPath<T, Date>`.
1313
- Proxy types: `FieldProxy`, `FormFieldProxy` and `ArrayProxy`.
14+
- `invalidateAll` option `force`, to always use the load function form data, instead of the one returned from the form action. Useful when updating the form data partially, to ensure that the data is refreshed from the server.
1415

1516
### Fixed
1617

src/lib/client/superForm.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export type FormOptions<
7575
> = Partial<{
7676
id: string;
7777
applyAction: boolean;
78-
invalidateAll: boolean;
78+
invalidateAll: boolean | 'force';
7979
resetForm: boolean | (() => boolean);
8080
scrollToError: 'auto' | 'smooth' | 'off' | boolean | ScrollIntoViewOptions;
8181
autoFocusOnError: boolean | 'detect';
@@ -865,7 +865,7 @@ export function superForm<
865865
Errors.set(output as ValidationErrors<T>);
866866
}
867867

868-
function Form_set(data: Partial<T>, options: FormDataOptions = {}) {
868+
function Form_set(data: T, options: FormDataOptions = {}) {
869869
// Check if file fields should be kept, usually when the server returns them as undefined.
870870
// in that case remove the undefined field from the new data.
871871
if (options.keepFiles) {
@@ -881,7 +881,7 @@ export function superForm<
881881
}
882882
});
883883
}
884-
return Form.update(($form) => ({ ...$form, ...data }), options);
884+
return Form.set(data, options);
885885
}
886886

887887
function Form_shouldReset(validForm: boolean, successActionResult: boolean) {
@@ -900,7 +900,9 @@ export function superForm<
900900
rebind({
901901
form,
902902
untaint: successResult,
903-
keepFiles: true
903+
keepFiles: true,
904+
// Check if the form data should be used for updating, or if the invalidateAll load function should be used:
905+
skipFormData: options.invalidateAll == 'force'
904906
});
905907
}
906908

@@ -1192,12 +1194,18 @@ export function superForm<
11921194
// tainted dialog when a form doesn't use it or the browser doesn't use JS.
11931195
options.taintedMessage = undefined;
11941196

1195-
function rebindPage(opts: {
1197+
// Role rebinding
1198+
function rebind(opts: {
11961199
form: SuperValidated<T, M, In>;
11971200
untaint: TaintedFields<T> | boolean;
1201+
message?: M;
11981202
keepFiles?: boolean;
1203+
posted?: boolean;
1204+
skipFormData?: boolean;
11991205
}) {
1206+
//console.log('🚀 ~ file: superForm.ts:721 ~ rebind ~ form:', form.data); //debug
12001207
const form = opts.form;
1208+
const message = opts.message ?? form.message;
12011209

12021210
if (opts.untaint) {
12031211
Tainted_set(typeof opts.untaint === 'boolean' ? undefined : opts.untaint, form.data);
@@ -1207,23 +1215,12 @@ export function superForm<
12071215
// Prevents object errors from being revalidated after rebind.
12081216
// Check if form was invalidated (usually with options.invalidateAll) to prevent data from being
12091217
// overwritten by the load function data
1210-
Form_set(form.data, { taint: 'ignore', keepFiles: opts.keepFiles });
1211-
}
1212-
1213-
// Role rebinding
1214-
function rebind(opts: {
1215-
form: SuperValidated<T, M, In>;
1216-
untaint: TaintedFields<T> | boolean;
1217-
message?: M;
1218-
keepFiles?: boolean;
1219-
posted?: boolean;
1220-
}) {
1221-
//console.log('🚀 ~ file: superForm.ts:721 ~ rebind ~ form:', form.data); //debug
1222-
1223-
rebindPage({ ...opts });
1224-
1225-
const form = opts.form;
1226-
const message = opts.message ?? form.message;
1218+
if (opts.skipFormData !== true) {
1219+
Form_set(form.data, {
1220+
taint: 'ignore',
1221+
keepFiles: opts.keepFiles
1222+
});
1223+
}
12271224

12281225
Message.set(message);
12291226
Errors.set(form.errors);

src/routes/(v2)/v2/Navigation.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
'index-errors',
3939
'onchange-target',
4040
'issue-356',
41-
'issue-358'
41+
'issue-358',
42+
'issue-360'
4243
].sort();
4344
</script>
4445

src/routes/(v2)/v2/issue-358/+page.server.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ export const actions = {
2020

2121
// Forgot to update email
2222
db.name = form.data.name;
23-
// @ts-expect-error Hack to prevent updating the missing schema field, it will be updated by the load function.
24-
delete form.data.email;
2523

2624
return message(form, 'Form posted successfully!');
2725
}

src/routes/(v2)/v2/issue-358/+page.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<script lang="ts">
2+
import { page } from '$app/stores';
23
import { superForm } from '$lib/index.js';
34
import SuperDebug from '$lib/index.js';
45
56
export let data;
67
8+
// eslint-disable-next-line svelte/valid-compile
9+
const resetForm = $page.url.searchParams.has('reset');
10+
711
const { form, errors, message, enhance } = superForm(data.form, {
8-
applyAction: false
12+
invalidateAll: 'force',
13+
resetForm
914
});
1015
</script>
1116

0 commit comments

Comments
 (0)