ozmat / elm-forms / Forms.Update

This module provides the update helpers. Please refer to the examples for a better understanding

Form Messages


type alias Msg comparable =
Internal.Msg comparable

These are the form Msg, they help updating a Field.

You need to implement them in your Msg, update function and use the helpers in your view events.

stringFieldMsg is for string Field (input, select) and boolFieldMsg is for bool Field (checkbox)

type YourMsg
    = SomeForm (Forms.Update.Msg comparable)
    | ....

yourUpdate : YourMsg -> Model -> Model
yourUpdate msg model =
    case msg of
        SomeForm formMsg ->
            ...

yourView : Model -> Html YourMsg
yourView model =
    ...
        -- select/input field
        onEvent (Forms.Update.stringFieldMsg SomeForm fieldKey)
        -- checkbox field
        onEvent (Forms.Update.boolFieldMsg SomeForm fieldKey)
    ...

Note: you will want to name your message according to the form it is handling in order to avoid confusion when using multiple forms

type YourMsg
    = RegisterForm (Forms.Update.Msg comparable)
    | LoginForm (Forms.Update.Msg comparable)
    | ...

stringFieldMsg : (Msg comparable -> msg) -> comparable -> String -> msg

Creates a form message that updates a string Field

boolFieldMsg : (Msg comparable -> msg) -> comparable -> Basics.Bool -> msg

Creates a form message that updates a bool Field

Form Update

updateForm : Msg comparable -> Forms.Form.Internal.Form comparable err a -> Forms.Form.Internal.Form comparable err a

This function will update a Form for you. Once you have implemented the form Msg, use it to update the Form

yourUpdate : YourMsg -> Model -> Model
yourUpdate msg model =
    case msg of
        SomeForm formMsg ->
            { model
                | yourForm = Forms.Update.updateForm formMsg model.yourForm
            }
        ...

Side effects

Sometimes you will need to have more control over a field update/event, that is what the side-effects helpers are for.

stringFieldCommands : model -> Msg comparable -> (model -> comparable -> String -> ( model, Platform.Cmd.Cmd msg )) -> ( model, Platform.Cmd.Cmd msg )

Helps defining Cmd on string Field events. You can use this function to run commands when an event is triggered on a specific string Field.

yourUpdate : YourMsg -> Model -> ( Model, Cmd YourMsg )
yourUpdate msg model =
    case msg of
        SomeForm formMsg ->
            let
                newModel =
                    { model
                        | yourForm = updateForm formMsg model.yourForm
                    }
            in
            stringFieldCommands newModel formMsg someFormCommands
        ...

someFormCommands : Model -> String -> String -> ( Model, Cmd YourMsg )
someFormCommands model key value =
    case key of
        "field-name" ->
            ( changeOrNotTheModel model
            , doSomeCommandsWithTheValue value
            )

        ...

        _ ->
            ( model
            , Cmd.none
            )

Note: this function only defines commands for the stringFieldMsg

boolFieldCommands : model -> Msg comparable -> (model -> comparable -> Basics.Bool -> ( model, Platform.Cmd.Cmd msg )) -> ( model, Platform.Cmd.Cmd msg )

Helps defining Cmd on bool Field events. You can use this function to run commands when an event is triggered on a specific bool Field.

Note: this function only defines commands for the boolFieldMsg

formCommands : model -> Msg comparable -> (model -> comparable -> String -> ( model, Platform.Cmd.Cmd msg )) -> (model -> comparable -> Basics.Bool -> ( model, Platform.Cmd.Cmd msg )) -> ( model, Platform.Cmd.Cmd msg )

Helps defining Cmd on both string and bool Field events.

formCommands
    model
    formMsg
    yourStringFieldCommands
    yourBoolFieldCommands