greg-enbala/elm-form/1.0.0

Unavailable. Nobody can download this version any more, with elm or with the Lamdera compiler: github.com/greg-enbala/elm-form is gone (deleted or private); Software Heritage has not archived the repo; no fork with tag 1.0.0 found (52 same-name repos checked); no cached copy. Use another version.

Details

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 Int
| Submit
| Validate
| Reset (List ( String, Field ))

Form messages for update.

= 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:

  • customError - a custom error type to extend built-in errors (set to () if you don't need it)
  • output - the type of the validation output.
type alias FieldState e a =
{ path : String
, value : Maybe a
, error : Maybe (ErrorValue e)
, liveError : Maybe (ErrorValue e)
, isDirty : Bool
, isChanged : Bool
, hasFocus : Bool
}

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

  • path - qualified path of the field in the form, with dots for nested fields (field.subfield)
  • value - a Maybe of the requested type
  • error - a Maybe of the field error
  • liveError - same but with added logic for live validation (see getLiveErrorAt impl)
  • isDirty - if the field content has been changed since last validation
  • isChanged - if the field value has changed since last init/reset
  • hasFocus - if the field is currently focused

Init/update lifecyle

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

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

update : 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 Bool

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

getListIndexes : String -> Form e o -> List 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 -> 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.