Skip to content

Commit eb10fd5

Browse files
committed
fix submitters on remote forms
1 parent fae6b5a commit eb10fd5

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

.changeset/nine-months-start.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+
fix: include `<button name="foo" value="bar">` values on submitted remote forms

documentation/docs/20-core-concepts/60-remote-functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ For client-side validation, you can specify a _preflight_ schema which will popu
427427

428428
const schema = v.object({
429429
title: v.pipe(v.string(), v.nonEmpty()),
430-
content:v.pipe(v.string(), v.nonEmpty())
430+
content: v.pipe(v.string(), v.nonEmpty())
431431
});
432432
</script>
433433

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,11 @@ export type RemoteForm<Input extends RemoteFormInput | void, Output> = {
19371937
/** Preflight checks */
19381938
preflight(schema: StandardSchemaV1<Input, any>): RemoteForm<Input, Output>;
19391939
/** Validate the form contents programmatically */
1940-
validate(options?: { includeUntouched?: boolean }): Promise<void>;
1940+
validate(options?: {
1941+
includeUntouched?: boolean;
1942+
/** Perform validation as if the form was submitted by the given button. */
1943+
submitter?: HTMLButtonElement | HTMLInputElement;
1944+
}): Promise<void>;
19411945
/** The result of the form submission */
19421946
get result(): Output | undefined;
19431947
/** The number of pending submissions */

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,11 @@ export function form(id) {
239239

240240
const form_data = new FormData(form);
241241

242+
const submitter_name = event.submitter?.getAttribute('name');
243+
if (submitter_name) {
244+
form_data.append(submitter_name, event.submitter?.getAttribute('value') ?? '');
245+
}
246+
242247
if (DEV) {
243248
validate_form_data(form_data, clone(form).enctype);
244249
}
@@ -429,13 +434,18 @@ export function form(id) {
429434
},
430435
validate: {
431436
/** @type {RemoteForm<any, any>['validate']} */
432-
value: async ({ includeUntouched = false } = {}) => {
437+
value: async ({ includeUntouched = false, submitter } = {}) => {
433438
if (!element) return;
434439

435440
const id = ++validate_id;
436441

437442
const form_data = new FormData(element);
438443

444+
const submitter_name = submitter?.getAttribute('name');
445+
if (submitter_name) {
446+
form_data.append(submitter_name, submitter?.getAttribute('value') ?? '');
447+
}
448+
439449
/** @type {readonly StandardSchemaV1.Issue[]} */
440450
let array = [];
441451

packages/kit/types/index.d.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1913,7 +1913,11 @@ declare module '@sveltejs/kit' {
19131913
/** Preflight checks */
19141914
preflight(schema: StandardSchemaV1<Input, any>): RemoteForm<Input, Output>;
19151915
/** Validate the form contents programmatically */
1916-
validate(options?: { includeUntouched?: boolean }): Promise<void>;
1916+
validate(options?: {
1917+
includeUntouched?: boolean;
1918+
/** Perform validation as if the form was submitted by the given button. */
1919+
submitter?: HTMLButtonElement | HTMLInputElement;
1920+
}): Promise<void>;
19171921
/** The result of the form submission */
19181922
get result(): Output | undefined;
19191923
/** The number of pending submissions */

0 commit comments

Comments
 (0)