dillonkearns / elm-form / Form.Handler

This API allows you to try parsing one of several forms.

This is useful if you want to

  1. Parse a form on the backend using code sharing to keep your backend and frontend validations in sync, and
  2. Parse in-flight form submissions to derive pending or optimistic UI state

elm-pages has some abstractions built around these ideas to help you receive form submissions on the backend, and automatically manage in-flight form submissions. However, you can manually wire this in or build similar abstractions in your own framework or application.

Example:

import Form
import Form.Field as Field
import Form.Handler exposing (Handler)
import Form.Validation as Validation

type Action =
    UpdateProfile ( String, String )
    | SendMessage ( String, String )


updateProfile : Form.HtmlForm String ( String, String ) input msg
updateProfile =
    Form.form
        (\first last ->
            { combine =
                Validation.succeed Tuple.pair
                    |> Validation.andMap first
                    |> Validation.andMap last
            , view = \_ -> []
            }
        )
        |> Form.field "first" (Field.text |> Field.required "Required")
        |> Form.field "last" (Field.text |> Field.required "Required")
        |> Form.hiddenKind ( "kind", "update-profile" ) "Expected kind"

sendMessage : Form.HtmlForm String ( String, String ) input msg
sendMessage =
    Form.form
        (\to body ->
            { combine =
                Validation.succeed Tuple.pair
                    |> Validation.andMap to
                    |> Validation.andMap body
            , view = \_ -> []
            }
        )
        |> Form.field "to" (Field.text |> Field.required "Required")
        |> Form.field "body" (Field.text |> Field.required "Required" |> Field.textarea { rows = Nothing, cols = Nothing })
        |> Form.hiddenKind ( "kind", "send-message" ) "Expected kind"

handler : Form.Handler.Handler String Action
handler =
    Form.Handler.init UpdateProfile updateProfile
    |> Form.Handler.with SendMessage sendMessage

Form.Handler.run
    [ ( "first", "Jane" )
    , ( "last", "Doe" )
    , ( "kind", "update-profile" )
    ]
    handler

--> Form.Valid (UpdateProfile ("Jane", "Doe") )

Form.Handler.run
    [ ( "to", "Jane" )
    , ( "body", "Hello!" )
    , ( "kind", "send-message" )
    ]
    handler

--> Form.Valid (SendMessage ("Jane", "Hello!") )


type Handler error parsed

A combined set of Form parsers which can be run with run

init : (parsed -> combined) -> Internal.Form.Form error { combineAndView | combine : Form.Validation.Validation error parsed kind constraints } parsed input -> Handler error combined

Start building a Handler.

with : (parsed -> combined) -> Internal.Form.Form error { combineAndView | combine : Form.Validation.Validation error parsed kind constraints } parsed input -> Handler error combined -> Handler error combined

Include an additional Form as one of the possibilities to parse into.

run : List ( String, String ) -> Handler error parsed -> Form.Validated error parsed

Parse your Handler with the given raw form data into a Validated value.