Skip to content

Commit b9635ab

Browse files
breaking: remove submitter option from experimental form validate(…) method, always provide default submitter (#14762)
* breaking: remove `submitter` option from experimental form `validate()` method, always provide default submitter * fix * remove log --------- Co-authored-by: Tee Ming <[email protected]>
1 parent bd55128 commit b9635ab

File tree

6 files changed

+20
-18
lines changed

6 files changed

+20
-18
lines changed

.changeset/tangy-aliens-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/kit': patch
3+
---
4+
5+
breaking: remove `submitter` option from experimental form `validate()` method, always provide default submitter

packages/kit/src/exports/public.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,8 +2067,6 @@ export type RemoteForm<Input extends RemoteFormInput | void, Output> = {
20672067
includeUntouched?: boolean;
20682068
/** Set this to `true` to only run the `preflight` validation. */
20692069
preflightOnly?: boolean;
2070-
/** Perform validation as if the form was submitted by the given button. */
2071-
submitter?: HTMLButtonElement | HTMLInputElement;
20722070
}): Promise<void>;
20732071
/** The result of the form submission */
20742072
get result(): Output | undefined;

packages/kit/src/runtime/client/remote-functions/form.svelte.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,15 +522,19 @@ export function form(id) {
522522
},
523523
validate: {
524524
/** @type {RemoteForm<any, any>['validate']} */
525-
value: async ({ includeUntouched = false, preflightOnly = false, submitter } = {}) => {
525+
value: async ({ includeUntouched = false, preflightOnly = false } = {}) => {
526526
if (!element) return;
527527

528528
const id = ++validate_id;
529529

530530
// wait a tick in case the user is calling validate() right after set() which takes time to propagate
531531
await tick();
532532

533-
const form_data = new FormData(element, submitter);
533+
const default_submitter = /** @type {HTMLElement | undefined} */ (
534+
element.querySelector('button:not([type]), [type="submit"]')
535+
);
536+
537+
const form_data = new FormData(element, default_submitter);
534538

535539
/** @type {InternalRemoteFormIssue[]} */
536540
let array = [];

packages/kit/test/apps/basics/src/routes/remote/form/validate/+page.svelte

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
const schema = v.object({
66
foo: v.picklist(['a', 'b', 'c']),
77
bar: v.picklist(['d', 'e']),
8-
button: v.optional(v.literal('submitter'))
8+
button: v.literal('submitter')
99
});
10-
let submitter;
10+
1111
let error = $state(false);
1212
</script>
1313

@@ -24,20 +24,17 @@
2424

2525
<input {...my_form.fields.bar.as('text')} />
2626

27-
<button>submit (imperative validation)</button>
27+
<button {...my_form.fields.button.as('submit', 'incorrect_value')}> submit </button>
2828

29-
<button bind:this={submitter} {...my_form.fields.button.as('submit', 'incorrect_value')}>
30-
submit
31-
</button>
3229
{#each my_form.fields.button.issues() as issue}
3330
<p>{issue.message}</p>
3431
{/each}
35-
</form>
3632

37-
<button
38-
id="trigger-validate"
39-
onclick={() => my_form.validate({ includeUntouched: true, submitter })}
40-
>
33+
<button {...my_form.fields.button.as('submit', 'submitter')}>
34+
submit (imperative validation)
35+
</button>
36+
</form>
37+
<button id="trigger-validate" onclick={() => my_form.validate({ includeUntouched: true })}>
4138
trigger validation
4239
</button>
4340

packages/kit/test/apps/basics/src/routes/remote/form/validate/form.remote.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const my_form = form(
66
v.object({
77
foo: v.picklist(['a', 'b', 'c']),
88
bar: v.picklist(['d', 'e', 'f']),
9-
button: v.optional(v.literal('submitter'))
9+
button: v.literal('submitter')
1010
}),
1111
async (data, issue) => {
1212
// Test imperative validation

packages/kit/types/index.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,8 +2043,6 @@ declare module '@sveltejs/kit' {
20432043
includeUntouched?: boolean;
20442044
/** Set this to `true` to only run the `preflight` validation. */
20452045
preflightOnly?: boolean;
2046-
/** Perform validation as if the form was submitted by the given button. */
2047-
submitter?: HTMLButtonElement | HTMLInputElement;
20482046
}): Promise<void>;
20492047
/** The result of the form submission */
20502048
get result(): Output | undefined;

0 commit comments

Comments
 (0)