Svelte · Middle · Question 36 of 75
How do you validate a form in a SvelteKit action and return errors?
import { fail } from "@sveltejs/kit";
export const actions = {
default: async ({ request }) => {
const data = await request.formData();
const email = data.get("email");
if (!email) return fail(400, { email, missing: true });
// ...
},
};- ✓Correct answer: Return
fail(status, data); the page receives it as theformprop - 2Throw
error(400)so the error page is shown - 3Return
{ status: 400 }
Explanation
Returning the submitted values lets the form keep what the user typed.