← Back to patterns
Server-side validation with invalid()
Business rules the schema can't know, reported per field
Pattern Usage:
export const orderHotcakes = form(schema, async (data, issue) => {
const stock = await getStockFromDb();
if (data.qty > stock)
invalid(issue.qty(`We only have ${stock} left`));
if (!validCoupon(data.coupon))
invalid(
issue.coupon('Unknown coupon'), // field-level
'Order not placed, check the coupon' // form-level
);
}); Live Demo:
🥞 Hotcakes in stock: 12
How it works: The second argument of a form handler is an issue proxy that mirrors your schema. invalid(issue.qty('…')) throws, like error() or redirect(), and the message appears on fields.qty.issues() exactly like a schema error.
Plain strings become form-level issues, visible only in fields.allIssues() (with an empty path). This form uses no enhance() at all: it also works without JavaScript.