Values from form fields are either String or List String
e.g. values for input [ type_ "number" ] [] will still be a String since
and is entirely up to your application to convert and validate it with
String.toInt or String.toFloat
Given a list of key values, combine the values of duplicate keys
import Dict exposing (Dict)
valuesDict
[ (1, OneValue "1")
, (2, OneValue "a")
, (2, ManyValues ["b","c"])
, (3, ManyValues ["yes","no"])
]
--> Dict.fromList
--> [ (1, OneValue "1")
--> , (2, ManyValues ["a","b","c"])
--> , (3, ManyValues ["yes","no"])
--> ]Given 2 Value, return a ManyValues
valuesAppend (OneValue "1") (OneValue "a")
--> ManyValues ["1","a"]
valuesAppend (OneValue "1") (ManyValues ["a","b"])
--> ManyValues ["1","a","b"]
valuesAppend (ManyValues ["1","2"]) (ManyValues ["a","b"])
--> ManyValues ["1","2","a","b"]
valuesAppend (ManyValues ["1","2"]) (OneValue "a")
--> ManyValues ["1","2","a"]OneValueExample usage
toColor : Maybe (NativeForm.Value String) -> Result String Color
toColor maybeV =
maybeV
|> Maybe.map (NativeForm.oneMap colorFromString)
|> Maybe.andThen (NativeForm.oneWithDefault Nothing)
|> Result.fromMaybe "invalid color"OneValue 3
|> oneMap ((+) 2)
--> OneValue 5
ManyValues [ 3 ]
|> oneMap ((+) 2)
--> ManyValues []OneValue 42
|> oneWithDefault 3
--> 42
ManyValues [42]
|> oneWithDefault 3
--> 3ManyValuesExample usage
toHobbies : Maybe (NativeForm.Value String) -> Result String (List Hobby)
toHobbies maybeV =
maybeV
|> Maybe.map (NativeForm.manyMap (List.filterMap hobbyFromString))
|> Maybe.map (NativeForm.manyWithDefault [])
|> Result.fromMaybe "invalid hobby"OneValue 3
|> manyMap ((++) [ 2 ])
--> ManyValues []
ManyValues [ 3 ]
|> manyMap ((++) [ 2 ])
--> ManyValues [ 2, 3 ]OneValue 42
|> manyWithDefault [3]
--> [3]
ManyValues [42]
|> manyWithDefault [3]
--> [42]
Given the
idattribute of a<form>tag, we can decode the current form values from the browserdocumentand into a List of key values.We are returning a
Listinstead ofDictbecause on form submit, duplicate names are preserved. So we are preserving them here too. If aDictis desired, use thevaluesDicthelper function