dwayne/elm-form/1.0.0

Form

Provides a way to package the fields, accessors, and business logic pertaining to a form behind a consistent interface.

Form

type Form state accessors error output
  • state: Typically a record containing fields and possibly other forms.
  • accessors: Typically a record containing getters/setters and anything else that needs access to the state of the form.
  • error: The errors produced by an invalid form.
  • output: The data produced by a valid form.

Construct

new :
{ init : state
, accessors : accessors
, validate : state -> Validation error output
}
-> Form state accessors error output

Accessors

type alias Accessor state a =
{ get : state -> a
, modify : (a -> a) -> state -> state
}

Combines a way to get a value from a property on state and a way to modify that property's value.

type alias State =
    { count : Int
    , name : String
    }


countAccessor : Accessor State Int
countAccessor =
    { get = .count
    , modify = \f state -> { state | count = f state.count }
    }


nameAccessor : Accessor State String
nameAccessor =
    { get = .name
    , modify = \f state -> { state | name = f state.name }
    }
get :
(accessors -> Accessor state a)
-> Form state accessors error output
-> a

Get the value of a property on the given form.

set :
(accessors -> Accessor state a)
-> a
-> Form state accessors error output
-> Form state accessors error output

Set the value of a property on the given form.

(accessors -> Accessor state a)
-> (a -> a)
-> Form state accessors error output
-> Form state accessors error output

Modify the value of a property on the given form.

(accessors -> state -> state)
-> Form state accessors error output
-> Form state accessors error output

Update the value of a property on the given form.

Query

isValid : Form state accessors error output -> Bool

True if the form does not have any errors.

isInvalid : Form state accessors error output -> Bool

True if the form has errors. isInvalid form is equivalent to not (isValid form).

Validate

validate : Form state accessors error output -> Validation error output
validateAsMaybe : Form state accessors error output -> Maybe output
validateAsResult : Form state accessors error output -> Result (List error) output

Convert

toState : Form state accessors error output -> state