ozmat/elm-forms/1.1.0

NOTE: this package is not compatible with Elm 0.19.1

Forms.Field

A Field represents a Form field. Please refer to the examples for a better understanding

Definition

type Field comparable
= FieldGroup (Group comparable)
| FieldValue Value

A Field can be a simple field with a Value (= FieldValue) or a group of fields (= FieldGroup)

FieldValue (String "some input value")


FieldGroup (Dict.fromList [ ... ])
type alias Group comparable =
Dict comparable (Field comparable)

A Group is a group of Fields. The underlying data structure is a Dict

Common Helpers

string : Field comparable

Is a shortcut to create a Field with the default String Value

string -- FieldValue (Forms.Value.defaultString)
bool : Field comparable

Is a shortcut to create a Field with the default Bool Value

bool -- FieldValue (Forms.Value.defaultBool)
group : List ( comparable, Field comparable ) -> Field comparable

Is a shortcut to create a Field with a Group of fields. The function takes a List of Tuple and creates the Group for you :

tupleExample : ( comparable, Field comparable )
tupleExample =
    (comparable, string)


group [tupleExample, ...] -- FieldGroup (Dict.fromList [ ... ])
fields : List ( comparable, Field comparable ) -> Group comparable

Is a shortcut to create a Group of Fields. The function takes a List of Tuple

someFormFields : Group comparable
someFormFields =
    fields
        [ ( comparable1, string )
        , ( comparable2, bool )
        , ( comparable3
          , group
                [ ( comparable4, string )
                , ( comparable5, string )
                , ( comparable6, string )
                ]
          )
        ]
stringWithValue : String -> Field comparable

Is a shortcut to create a Field with an initial String Value

stringWithValue "initial" -- FieldValue (String "initial")
boolWithValue : Bool -> Field comparable

Is a shortcut to create a Field with an initial Bool Value

boolWithValue True -- FieldValue (Bool True)

Search

getValue : comparable -> Group comparable -> Maybe Value

Retrieves the Value associated with a key. If the key is not found or the Field is not a FieldValue returns Nothing

someFormFields : Group comparable
someFormFields =
    fields
        [ ( comparable1, string )
        , ( comparable2, bool )
        , ( comparable3, group [] )
        ]


getValue comparable1 someFormFields -- Just (String ...)
getValue comparable2 someFormFields -- Just (Bool ...)
getValue comparable3 someFormFields -- Nothing
getValue notfound someFormFields    -- Nothing
getGroup : comparable -> Group comparable -> Maybe (Group comparable)

Retrieves the Group associated with a key. If the key is not found or the Field is not a FieldGroup returns Nothing

someFormFields : Group comparable
someFormFields =
    fields
        [ ( comparable1, string )
        , ( comparable2, bool )
        , ( comparable3, group [] )
        ]


getGroup comparable1 someFormFields -- Nothing
getGroup comparable2 someFormFields -- Nothing
getGroup comparable3 someFormFields -- Just (Dict.fromList [ ... ])
getGroup notfound someFormFields    -- Nothing
setValue : comparable -> Value -> Group comparable -> Group comparable

Updates the Value associated with a key. It will only update the Value if the key is found, the Field is a FieldValue and the Values have the same type

someFormFields : Group comparable
someFormFields =
    fields
        [ ( comparable1, string )
        , ( comparable2, group [] )
        ]


setValue comparable1 (String ...) someFormFields -- updates
setValue comparable1 (Bool ...) someFormFields   -- doesn't update
setValue comparable2 (...) someFormFields        -- doesn't update
setValue notfound (...) someFormFields           -- doesn't update