SvelteKit
Since this is a Svelte
library, you may want to use it with the SvelteKit
.
With this package you will be able to perform server-side validation of form data
even if the user has JavaScript
disabled.
Installation
Example
Server
On the server side you should implement at least one action
which will always return the result of validateForm
function (redirect
and error
helpers are allowed).
If the form data is passed as FormData
, you need to create a parser for it.
If you want to populate the form from the database,
you can use the initForm
function inside the load
function.
Client
On the client side you should call useSvelteKitForm
.
According to Svelte documentation your form should always use POST
requests.
Progressive enhancement
You should always use enhance
action on your forms.
But if you really want the form to work with JavaScript
disabled, you should consider the following limitation:
validateForm
should be called withsendData: true
- Form elements for
oneOf
,anyOf
,dependencies
,additionalProperties
andadditionalItems
will not update their state. - Some widgets (like multiselect, depends on the theme) may will not work, because they require
JavaScript
. - Conversion from
FormData
toJSON
can happen with data loss. This conversion relies on the field names computation algorithm and it may lead to ambiguous results if the following conditions are violated:- Id prefix and separators:
- Must be non empty, non numeric string
- Must not include each other
- Property names and keys of default object values in your schema must not contain the separators.
If they do you can change the default separators with
idSeparator
andisPseudoSeparator
options. Default separator values:idSeparator
-.
isPseudoSeparator
-::
- If your schema contains
additionalProperties
:- Keys of initial object values must not contain the separators.
- If you provide some initial value for
additionalProperties
orJavaScript
is enabled but the form is used withoutenhance
:- You should provide
additionalPropertyKeyValidationError
oradditionalPropertyKeyValidator
options touseSvelteKitForm
for preventing the user to input invalid keys.
- You should provide
- You may produce these checks at runtime with the
staticAnalysis
and other functions from@sjsf/form/static-analysis
submodule. Functions from this submodule returns an iterable of errors, so you can:- Throw an error on the first error or list all errors (in a dev environment)
- Check if the schema and id separator is correct and save the original form data if there are errors (in a production environment)
- Id prefix and separators: