@@ -6,10 +6,12 @@ sidebar:
66
77import { Code , Tabs , TabItem } from " @astrojs/starlight/components" ;
88import { DEFAULT_ID_SEPARATOR , DEFAULT_PSEUDO_ID_SEPARATOR } from ' @sjsf/form' ;
9+ import { IDENTIFIABLE_FIELD_ELEMENTS } from ' @sjsf/sveltekit'
910
1011import Npm from ' @/components/npm.astro' ;
1112
1213import clientCode from ' ./_sveltekit-client.api.svelte?raw' ;
14+ import clientFlexCode from ' ./_sveltekit-flex-client.api.svelte?raw'
1315import serverCode from ' ./_sveltekit-server.api.ts?raw' ;
1416
1517:::caution
@@ -20,7 +22,7 @@ SvelteKit integration is experimental
2022
2123Since this is a ` Svelte ` library, you may want to use it with the ` SvelteKit ` .
2224
23- With this package you will be able to perform server-side validation of form data
25+ With this package you will be able to perform server-side validation of the form data
2426even if the user has ` JavaScript ` disabled.
2527
2628## Installation
@@ -29,6 +31,8 @@ even if the user has `JavaScript` disabled.
2931
3032## Example
3133
34+ See details in the sections below
35+
3236<Tabs >
3337 <TabItem label = ' +page.svelte' >
3438 <Code code = { clientCode } lang = ' svelte' />
@@ -41,38 +45,11 @@ even if the user has `JavaScript` disabled.
4145## Server
4246
4347On the server side you should implement at least one [ action] ( https://svelte.dev/docs/kit/form-actions )
44- which will always return the result of ` validateForm ` function (` redirect ` and ` error ` helpers are allowed).
48+ which will always return the result of ` validateForm2 ` function (` redirect ` and ` error ` helpers are allowed).
4549
4650If the form data is passed as ` FormData ` , you need to create a parser for it.
4751
48- ``` typescript
49- import { makeFormDataParser , validateForm } from ' @sjsf/sveltekit/server' ;
50-
51- const validator = createValidator ();
52-
53- const parseFormData = makeFormDataParser ({
54- validator
55- });
56-
57- export const actions = {
58- default : async (event ) => {
59- const form = validateForm ({
60- schema ,
61- validator ,
62- data: await parseFormData ({
63- schema ,
64- request ,
65- }),
66- })
67- if (! form .isValid ) {
68- return fail (400 , { form });
69- }
70- return {
71- form ,
72- };
73- },
74- } satisfies Actions ;
75- ```
52+ <Code code = { serverCode } lang = " typescript" />
7653
7754:::note
7855
@@ -101,46 +78,30 @@ The name of the `initForm` function result should match the name from the `actio
10178
10279## Client
10380
104- On the client side you should call ` useSvelteKitForm ` .
105-
106- ``` typescript
107- import { useSvelteKitForm , meta } from ' @sjsf/sveltekit/client' ;
108-
109- import type { PageData , ActionData } from ' ./$types' ;
81+ On the client side you can use ` SvelteKitForm ` component (see example).
82+ Or use more flexible solution:
11083
111- const { form, mutation, enhance } = useSvelteKitForm (
112- meta <ActionData , PageData >(), " form" , { /* form options */ }
113- );
114- ```
84+ <Code code = { clientFlexCode } lang = ' svelte' />
11585
11686:::note
11787
118- The error type will be inferred from ` ActionData ` and the form data type from ` PageData ` .
88+ The error type will be inferred from the ` ActionData ` type and the form data type from the ` PageData ` type .
11989
12090If you did not use ` initForm ` in the ` load ` function,
121- the third generic parameter of the ` meta ` function will be used as the form data type
122- (the default is ` SchemaValue ` ).
91+ the fourth generic parameter of the ` createMeta ` function will be used as the form data type
92+ (default type is ` SchemaValue ` ).
12393
12494:::
12595
12696According to Svelte documentation your form should always use ` POST ` requests.
12797
128- ``` svelte
129- <form use:enhance method="POST" novalidate style="display: flex; flex-direction: column; gap: 1rem">
130- <FormContent bind:value={form.formValue} />
131- <button type="submit" style="padding: 0.5rem;">Submit</button>
132- </form>
133- ```
134-
13598### Progressive enhancement
13699
137- You should always use ` enhance ` action on your forms.
138-
139- But if you really want the form to work with ` JavaScript ` disabled, you should consider the following limitation:
100+ By default, the form will work even with JavaScript disabled, but you should consider the following limitations of this mode of operation:
140101
141- - ` validateForm ` should be called with ` sendData: true `
142- - Form elements for ` oneOf ` , ` anyOf ` , ` dependencies ` , ` additionalProperties ` and ` additionalItems `
143- will not update their state.
102+ - ` validateForm2 ` should be called with ` sendData: true ` to persist form data between page updates
103+ - Form fields for ` oneOf ` , ` anyOf ` , ` dependencies ` , ` additionalProperties ` and ` additionalItems `
104+ will not expand/switch their state.
144105- Some widgets (like multiselect, depends on the theme) may will not work, because they require ` JavaScript ` .
145106- Conversion from ` FormData ` to ` JSON ` can happen with data loss.
146107This conversion relies on the field names computation algorithm and
@@ -155,8 +116,12 @@ it may lead to ambiguous results if the following conditions are violated:
155116 - ` isPseudoSeparator ` - <code >{ DEFAULT_PSEUDO_ID_SEPARATOR } </code >
156117 - If your schema contains ` additionalProperties ` :
157118 - Keys of initial object values must not contain the separators.
158- - If you provide some initial value for ` additionalProperties ` or ` JavaScript ` is enabled but the form is used without ` enhance ` :
119+ - If you provide some initial value for ` additionalProperties ` or ` JavaScript ` is enabled but the raw ` form ` element is used ( without ` use:form. enhance` ) :
159120 - You should provide ` additionalPropertyKeyValidationError ` or ` additionalPropertyKeyValidator ` options to ` useSvelteKitForm ` for preventing the user to input invalid keys.
121+ Additional property name is invalid if:
122+ - ` additionalProperties ` schema is an object or an array schema and name contains ` idSeparator `
123+ - ` additionalProperties ` schema is neither an object schema nor an array schema and name has a suffix consisting of a ` isPseudoSeparator `
124+ and a keyword: { IDENTIFIABLE_FIELD_ELEMENTS .map (el => <><code >{ el } </code ><span > </span ></>)}
160125 - You may produce these checks at runtime with the ` staticAnalysis ` and other functions from ` @sjsf/form/static-analysis ` submodule.
161126 Functions from this submodule returns an iterable of errors, so you can:
162127 - Throw an error on the first error or list all errors (in a dev environment)
0 commit comments