Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tangy-aliens-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

breaking: remove `submitter` option from experimental form `validate()` method, always provide default submitter
2 changes: 0 additions & 2 deletions packages/kit/src/exports/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2053,8 +2053,6 @@ export type RemoteForm<Input extends RemoteFormInput | void, Output> = {
includeUntouched?: boolean;
/** Set this to `true` to only run the `preflight` validation. */
preflightOnly?: boolean;
/** Perform validation as if the form was submitted by the given button. */
submitter?: HTMLButtonElement | HTMLInputElement;
}): Promise<void>;
/** The result of the form submission */
get result(): Output | undefined;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,15 +519,19 @@ export function form(id) {
},
validate: {
/** @type {RemoteForm<any, any>['validate']} */
value: async ({ includeUntouched = false, preflightOnly = false, submitter } = {}) => {
value: async ({ includeUntouched = false, preflightOnly = false } = {}) => {
if (!element) return;

const id = ++validate_id;

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

const form_data = new FormData(element, submitter);
const default_submitter = /** @type {HTMLElement | undefined} */ (
element.querySelector('button, [type="submit"]')
);

const form_data = new FormData(element, default_submitter);

/** @type {InternalRemoteFormIssue[]} */
let array = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
const schema = v.object({
foo: v.picklist(['a', 'b', 'c']),
bar: v.picklist(['d', 'e']),
button: v.optional(v.literal('submitter'))
button: v.literal('submitter')
});
let submitter;

$inspect(my_form.fields.allIssues());
</script>

Expand All @@ -24,19 +24,17 @@

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

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

<button bind:this={submitter} {...my_form.fields.button.as('submit', 'incorrect_value')}>
submit
</button>
{#each my_form.fields.button.issues() as issue}
<p>{issue.message}</p>
{/each}

<button {...my_form.fields.button.as('submit', 'submitter')}>
submit (imperative validation)
</button>
</form>
<button
id="trigger-validate"
onclick={() => my_form.validate({ includeUntouched: true, submitter })}
>
<button id="trigger-validate" onclick={() => my_form.validate({ includeUntouched: true })}>
trigger validation
</button>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const my_form = form(
v.object({
foo: v.picklist(['a', 'b', 'c']),
bar: v.picklist(['d', 'e', 'f']),
button: v.optional(v.literal('submitter'))
button: v.literal('submitter')
}),
async (data, invalid) => {
// Test imperative validation
Expand Down
2 changes: 0 additions & 2 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2029,8 +2029,6 @@ declare module '@sveltejs/kit' {
includeUntouched?: boolean;
/** Set this to `true` to only run the `preflight` validation. */
preflightOnly?: boolean;
/** Perform validation as if the form was submitted by the given button. */
submitter?: HTMLButtonElement | HTMLInputElement;
}): Promise<void>;
/** The result of the form submission */
get result(): Output | undefined;
Expand Down
Loading