Parse form requests
Simple form exposes helpers to parse and validate form requests with generic APIs and the Astro.locals.form
object when using Astro frontmatter.
validateForm()
Type: validateForm<T extends ZodRawShape>(params: { formData: FormData, validator: T): Promise<GetDataResult<T>>
The validateForm()
function can be used to validate any form request using a simple form validator. This returns data parsed by your validator or a fieldErrors
object:
Astro.locals.form
Simple form exposes helpers to parse form requests from your Astro frontmatter.
getData()
Type: getData<T extends { validator: FormValidator }>(form: T): Promise<GetDataResult<T["validator"]> | undefined>
Astro.locals.form.getData()
parses any incoming form request with the method POST. This will return undefined
if no form request was sent, or return form data parsed by your Zod validator.
If successful, result.data
will contain the parsed result. Otherwise, result.fieldErrors
will contain validation error messages by field name:
getDataByName()
Type: getDataByName<T extends { validator: FormValidator }>(name: string, form: T): Promise<GetDataResult<T["validator"]> | undefined>
You may have multiple forms on the page you want to parse separately. You can define a unique form name in this case, and pass the name as a hidden input within the form using <FormName>
: