11---
22title : SvelteKit
33sidebar :
4- order : 1
4+ order : 2
55---
66
77import { Code , Tabs , TabItem } from " @astrojs/starlight/components" ;
@@ -37,10 +37,27 @@ even if the user has `JavaScript` disabled.
3737On the server side you should implement at least one [ action] ( https://svelte.dev/docs/kit/form-actions )
3838which will always return the result of ` validateForm ` function (` redirect ` and ` error ` helpers are allowed).
3939
40+ If form data is passed via ` FormData ` you should create a parser for it.
41+
4042``` typescript
43+ import { makeFormDataParser , validateForm } from ' @sjsf/sveltekit/server' ;
44+
45+ const validator = createValidator ();
46+
47+ const parseFormData = makeFormDataParser ({
48+ validator
49+ });
50+
4151export const actions = {
4252 default : async (event ) => {
43- const form = validateForm ({/* ... */ })
53+ const form = validateForm ({
54+ schema ,
55+ validator ,
56+ data: await parseFormData ({
57+ schema ,
58+ request ,
59+ }),
60+ })
4461 if (! form .isValid ) {
4562 return fail (400 , { form });
4663 }
@@ -60,6 +77,8 @@ You can use any convenient name instead of `form`
6077If you want to populate form from database, you can use ` initForm ` function inside the ` load ` function.
6178
6279``` typescript
80+ import { initForm } from ' @sjsf/sveltekit/server' ;
81+
6382export const load = async () => {
6483 const data = await getData ();
6584 const form = initForm ({ schema , validator , initialValue: data });
@@ -78,6 +97,8 @@ The name of the `initForm` function result should match the name from the `actio
7897On the client side you should call ` useSvelteKitForm ` .
7998
8099``` typescript
100+ import { useSvelteKitForm , meta } from ' @sjsf/sveltekit/client' ;
101+
81102import type { PageData , ActionData } from ' ./$types' ;
82103
83104const { form, mutation, enhance } = useSvelteKitForm (
@@ -132,4 +153,4 @@ it may lead to ambiguous results if the following conditions are violated:
132153 - You may produce these checks at runtime with the ` staticAnalysis ` and other functions from ` @sjsf/form/static-analysis ` submodule.
133154 Functions from this submodule returns an iterable of errors, so you can:
134155 - Throw an error on the first error or list all errors (in a dev environment)
135- - Check if the data in the server action is correct and save the original form data if there are errors (in a production environment)
156+ - Check if the schema and id separator is correct and save the original form data if there are errors (in a production environment)
0 commit comments