formAction$
Creates an action for progressively enhanced forms that handles validation and submission on the server.
// Form action for simple forms
formAction$<Values, ResponseData>(action, validate);
// Form actions for complex forms
formAction$<Values, ResponseData>(action, options);
Generics
Values
FieldValues
ResponseData
Maybe<
ResponseData
> =undefined
Explanation
If you use TypeScript, you should define your field values. This way the library knows exactly which fields your form has and what values they expect. It also allows autocompletion and TypeScript notifies you of typos.
It is recommended to use
type
instead ofinterface
as this currently requires an explicit index signature in TypeScript. More about this here.
Parameters
action
(values:
FieldValues
, event:RequestEventAction
) =>MaybePromise<
FormActionResult | void
>validate
QRL<
ValidateForm
>options
object
validate
QRL<
ValidateForm
>arrays
Maybe<
>string
[]booleans
Maybe<
>string
[]files
Maybe<
>string
[]numbers
Maybe<
>string
[]
Explanation
The action
function is only executed if the form values have passed the validation. Since validate
is executed on the server, the validation cannot be manipulated by the user. You can use the action
function to process the form values and store them in a database, for example. Also you can manipulate the state of the form action via the return value or send a message to the client via the FormResponse
object.
Using arrays
, booleans
, files
and numbers
you can suplement information that is lost when submitting the form without JavaScript. You can find more about this here.