Skip to content
Merged
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/lucky-pandas-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: don't throw from remote form `validate()` if the form unmounts while it is waiting for a tick
Original file line number Diff line number Diff line change
Expand Up @@ -695,13 +695,14 @@ export function form(id) {
validate: {
/** @type {RemoteForm<any, any>['validate']} */
value: async ({ all = 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();

// the form may have been removed from the DOM while we were waiting
if (!element) return;

const default_submitter = /** @type {HTMLElement | undefined} */ (
element.querySelector('button:not([type]), [type="submit"]')
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import { issue_path_form, my_form, my_form_2 } from './form.remote.ts';
import { issue_path_form, my_form, my_form_2, unmount_form } from './form.remote.ts';
import * as v from 'valibot';

const schema = v.object({
Expand All @@ -8,7 +8,13 @@
button: v.literal('submitter')
});

const unmount_schema = v.object({
qux: v.picklist(['a', 'b'])
});

let error = $state(false);
let mounted = $state(true);
let unmount_error = $state('no error');
</script>

<form
Expand Down Expand Up @@ -72,3 +78,26 @@

<button>submit</button>
</form>

{#if mounted}
<form id="unmount-form" {...unmount_form.preflight(unmount_schema)}>
<input {...unmount_form.fields.qux.as('text')} />
</form>
{/if}

<button
id="unmount-then-validate"
onclick={async () => {
const validated = unmount_form.validate({ all: true });
mounted = false;

try {
await validated;
} catch (e) {
unmount_error = /** @type {Error} */ (e).message;
}
}}
>
unmount then validate
</button>
<p id="unmount-error">{unmount_error}</p>
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ export const issue_path_form = form(
return data;
}
);

export const unmount_form = form(
v.object({
qux: v.picklist(['a', 'b'])
}),
async (data) => {
return data;
}
);
14 changes: 14 additions & 0 deletions packages/kit/test/apps/async/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,20 @@ test.describe('remote functions', () => {
await expect(allIssues).toContainText('"path":["nested","value"]');
});

test('form validate does not throw if the form unmounts while validating', async ({
page,
javaScriptEnabled
}) => {
if (!javaScriptEnabled) return;

await page.goto('/remote/form/validate');

await page.locator('#unmount-then-validate').click();

await expect(page.locator('#unmount-form')).toHaveCount(0);
await expect(page.locator('#unmount-error')).toHaveText('no error');
});

test('form validation issues cleared', async ({ page, javaScriptEnabled }) => {
if (!javaScriptEnabled) return;

Expand Down
Loading