jwheeler-cp / elm-form / Form

Simple forms made easy: A Dict implementation of the core Json.Decode API, with state lifecycle and input helpers for the views.

Types


type Msg
    = NoOp
    | Focus String
    | Blur String
    | Input String InputType FieldValue
    | Append String
    | RemoveItem String Basics.Int
    | Submit
    | Validate
    | Reset (List ( String, Field ))

Form messages for update.


type InputType
    = Text
    | Textarea
    | Select
    | Radio
    | Checkbox

Input types to determine live validation behaviour.


type Form customError output

Form to embed in your model. Type parameters are:


type alias FieldState e a =
{ path : String
, value : Maybe a
, error : Maybe (ErrorValue e)
, liveError : Maybe (ErrorValue e)
, isDirty : Basics.Bool
, isChanged : Basics.Bool
, hasFocus : Basics.Bool 
}

Field state containing all necessary data for view and update, can be retrived with Form.getFieldAsString or Form.getFieldAsBool.

Init/update lifecyle

initial : List ( String, Field ) -> Validate.Validation e output -> Form e output

Initial form state. See Form.Field for initial fields, and Form.Validate for validation.

update : Validate.Validation e output -> Msg -> Form e output -> Form e output

Update form state with the given message

Field state accessors

getFieldAsString : String -> Form e o -> FieldState e String

Get field state at path, with value as a String.

getFieldAsBool : String -> Form e o -> FieldState e Basics.Bool

Get field state at path, with value as a Bool.

getListIndexes : String -> Form e o -> List Basics.Int

return a list of indexes so one can build qualified names of fields in list.

Global state accessors

getFocus : Form e o -> Maybe String

Return currently focused field, if any.

isSubmitted : Form e o -> Basics.Bool

Get form submission state. Useful to show errors on unchanged fields.

getErrors : Form e o -> List ( String, ErrorValue e )

Get list of errors on qualified paths.

getOutput : Form e o -> Maybe o

Get form output, in case of validation success.

getChangedFields : Form e o -> Set String

Get set of changed fields.