← Back to patterns

Client-side preflight

Validate in the browser with .preflight(schema) before anything is sent

Pattern Usage:

// schema.ts (shared, not a .remote file)
export const signupSchema = z.object({ ... });

// signup.remote.ts
export const signup = form(signupSchema, async (data, issue) => {
  if (taken.has(data.username))
    invalid(issue.username('already taken'));
});

// +page.svelte
const form = signup.preflight(signupSchema);

<form {...form} oninput={() => form.validate()}>
<button onclick={() => form.validate({ all: true })}>

Live Demo:

try "admin" to hit the server check

Requests sent: 0

How it works: .preflight(schema) runs the schema in the browser. Invalid submissions are blocked before a request is made; watch the counter. The server still validates with the same schema, because a client can always be bypassed.

validate() on input only reports fields you have already touched; validate({ all: true }) reports all of them. Checks only the server can do (is the name taken?) come back via invalid(issue.username(...)). Fields starting with _ are never sent back to repopulate the form.