dillonkearns / elm-pages-v3-beta / Scaffold.Form

This module helps you with scaffolding a form in elm-pages, similar to how rails generators are used to scaffold out forms to get up and running quickly with the starting point for a form with different field types. See also Scaffold.Route.

See the AddRoute script in the starter template for an example. It's usually easiest to modify that script as a starting point rather than using this API from scratch.

Using the AddRoute script from the default starter template, you can run a command like this:

npx elm-pages run AddRoute Profile.Username_.Edit first last bio:textarea dob:date to generate a Route module app/Route/Profile/Username_/Edit.elm with the wiring form a Form.

Learn more about writing and running elm-pages Scripts for scaffolding.


type Kind
    = FieldInt
    | FieldText
    | FieldTextarea
    | FieldFloat
    | FieldTime
    | FieldDate
    | FieldCheckbox

provide : { fields : List ( String, Kind ), elmCssView : Basics.Bool, view : { formState : Context, params : List { name : String, kind : Kind, param : Elm.Expression } } -> Elm.Expression } -> Maybe { formHandlers : Elm.Expression, form : Elm.Expression, declarations : List Elm.Declaration }

restArgsParser : Cli.Option.Option (List String) (List ( String, Kind )) Cli.Option.RestArgsOption

This parser handles the following field types (or text if none is provided):

The naming convention follows the same naming as the HTML form field elements or attributes that are used to represent them. In addition to using the appropriate field type, this will also give you an Elm type with the corresponding base type (like Date for date or Bool for checkbox).


type alias Context =
{ errors : Elm.Expression
, submitting : Elm.Expression
, submitAttempted : Elm.Expression
, data : Elm.Expression
, expression : Elm.Expression 
}

recordEncoder : Elm.Expression -> List ( String, Kind ) -> Elm.Expression

Generate a JSON Encoder for the form fields. This can be helpful for sending the validated form data through a BackendTask.Custom or to an external API from your scaffolded Route Module code.

fieldEncoder : Elm.Expression -> String -> Kind -> Elm.Expression

A lower-level, more granular version of recordEncoder - lets you generate a JSON Encoder Expression for an individual Field rather than a group of Fields.