Form Actions
Form actions let you handle <form> submissions on the server, with built-in validation patterns.
Defining Actions
Section titled “Defining Actions”Export an actions object from +page.server.ts:
import { fail } from "bosia";import type { RequestEvent } from "bosia";
export async function load() { return { greeting: "Contact us" };}
export const actions = { default: async ({ request }: RequestEvent) => { const data = await request.formData(); const email = data.get("email") as string; const name = data.get("name") as string;
const errors: Record<string, string> = {}; if (!email) errors.email = "Email is required"; if (!name) errors.name = "Name is required";
if (Object.keys(errors).length > 0) { return fail(400, { email, name, errors }); }
// Process the form... return { success: true, email, name }; },};Default Action
Section titled “Default Action”A <form method="POST"> with no action attribute hits the default action:
<form method="POST"> <input name="name" value={form?.name ?? ""} /> <input name="email" value={form?.email ?? ""} /> <button type="submit">Submit</button></form>Named Actions
Section titled “Named Actions”Use the action attribute with a ?/ prefix to target a specific action:
<form method="POST" action="?/reset"> <button type="submit">Reset</button></form>export const actions = { default: async ({ request }: RequestEvent) => { // ... }, reset: async () => { return { cleared: true }; },};Validation with fail()
Section titled “Validation with fail()”fail() returns an ActionFailure — it’s returned, not thrown:
import { fail } from "bosia";
// Returns a 400 response with the error datareturn fail(400, { email, // preserve user input name, errors: { email: "Invalid email format" },});Accessing Action Data
Section titled “Accessing Action Data”The action result is available as the form prop:
<script lang="ts"> let { data, form } = $props();</script>
{#if form?.errors} <p class="text-red-500">{form.errors.email}</p>{/if}
{#if form?.success} <p class="text-green-500">Submitted successfully!</p>{/if}Redirects from Actions
Section titled “Redirects from Actions”Use redirect() to navigate after a successful action:
import { redirect } from "bosia";
export const actions = { default: async ({ request }: RequestEvent) => { // Process form... redirect(303, "/thank-you"); },};How It Works
Section titled “How It Works”- Browser submits the form as a standard POST request
- Bosia calls the matching action function
- On success: the page re-renders with the action return value as
formprop and freshload()data - On fail(): the page re-renders with the failure data as
formprop at the specified status code - On redirect(): the browser follows the redirect