Skip to content

Lesson 6 of 9 · 25m

Form Actions: Mutations That Work Before JavaScript Loads

Progressively enhanced forms, validation with fail(), multiple actions, use:enhance, and the double-submit details users notice.

SvelteKit's form actions are not a nostalgic trick for no-JavaScript demos. They are a disciplined mutation boundary: the browser posts real form data, the server validates it, and the same page re-renders with errors or fresh data. JavaScript then enhances the interaction instead of becoming a prerequisite for using the product.

A real form is the baseline

<form method="POST" use:enhance>
  <label>Display name <input name="displayName" value={form?.values?.displayName ?? data.user.displayName} /></label>
  {#if form?.errors?.displayName}
    <p role="alert">{form.errors.displayName}</p>
  {/if}
  <button>Save</button>
</form>

Because it is a real <form>, Enter submits it, labels work, password managers recognize it, and a slow network does not leave the UI in a mystery state.

The action owns validation and writes

// src/routes/settings/profile/+page.server.ts
import { fail, redirect } from "@sveltejs/kit";
export const actions = {
  default: async ({ request, locals }) => {
    const data = await request.formData();
    const displayName = String(data.get("displayName") ?? "").trim();
    if (displayName.length < 2) {
      return fail(400, { errors: { displayName: "Use at least two characters." }, values: { displayName } });
    }
    await locals.db.users.update(locals.user.id, { displayName });
    redirect(303, "/settings/profile");
  },
};

fail(status, payload) returns validation failure without making it a server crash. The page receives it as the form prop. Return the submitted values with errors so users do not lose their work.

Enhance, don't replace

Import enhance from $app/forms and attach it with use:enhance. The enhancer intercepts the post, submits it without a full navigation, and applies the server result; its callback can toggle a pending state around await update(). The no-JavaScript path remains intact underneath. Do not use enhancement as permission to skip server validation — client convenience never validates for the internet.

Multiple actions use named submitters

<button formaction="?/archive">Archive</button>
<button formaction="?/delete" class="danger">Delete</button>

Those map to actions.archive and actions.delete. Keep destructive actions separate and re-authorize them server-side. A hidden field is not authorization.

Field notes

  • Redirect after successful mutation with 303 so refresh does not repost the form.
  • Disable submit controls while enhanced submission is pending; native no-JS posts are already serialized by the browser.
  • File inputs belong in real forms. request.formData() handles multipart bodies without bespoke JSON encoding.
  • Never trust formData types blindly: everything arrives as a string or File, and String(value) is not validation.

Was this lesson helpful?

Sign in to rate