0Pricing
Sveltejs Academy · Lesson

Handling Validation Errors in Actions

Return validation errors and repopulate form fields using fail() and ActionData.

The fail Helper

Use fail() to return validation errors with a non-2xx status without throwing.

import { fail } from "@sveltejs/kit";
if (!email) return fail(400, { email, missing: true });

Status Code

First argument is the HTTP status (usually 400 for validation).

All lessons in this course

  1. +page.server.js with actions
  2. The use:enhance Directive
  3. Progressive Enhancement for Forms
  4. Handling Validation Errors in Actions
← Back to Sveltejs Academy