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 the form data
even if the user has JavaScript
disabled.
Installation
Example
See details in the sections below
Server
On the server side you should implement at least one action
which will always return the result of validateForm2
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 can use SvelteKitForm
component (see example).
Or use more flexible solution:
According to Svelte documentation your form should always use POST
requests.
Progressive enhancement
By default, the form will work even with JavaScript disabled, but you should consider the following limitations of this mode of operation:
validateForm2
should be called withsendData: true
to persist form data between page updates- Form fields for
oneOf
,anyOf
,dependencies
,additionalProperties
andadditionalItems
will not expand/switch 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 rawform
element is used (withoutuse:form.enhance
):- You should provide
additionalPropertyKeyValidationError
oradditionalPropertyKeyValidator
options touseSvelteKitForm
for preventing the user to input invalid keys. Additional property name is invalid if:additionalProperties
schema is an object or an array schema and name containsidSeparator
additionalProperties
schema is neither an object schema nor an array schema and name has a suffix consisting of aisPseudoSeparator
and a keyword:key-input
anyof
examples
help
oneof
- 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: