Last active
July 7, 2016 12:13
-
-
Save simonh1000/67c1c98c8c7c7a63019bd4a7a9f85ec7 to your computer and use it in GitHub Desktop.
Form errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Test exposing (..) | |
import Platform.Cmd exposing (Cmd) | |
import Html exposing (..) | |
import Html.App as Html | |
import Html.Attributes exposing (..) | |
import Form exposing (..) | |
import Form.Input as Input | |
import Form.Field as Field | |
import Form.Validate as V exposing (..) | |
import Form.Error exposing (..) | |
import List as L | |
type CustomError = Yay | Ooops | |
type alias Model = | |
Form CustomError FormItems | |
init = | |
Form.initial [] validation | |
type alias FormItems = | |
{ fold : String | |
, pageCount : Float | |
} | |
type Msg | |
= FormMsg Form.Msg | |
update : Msg -> Model -> (Model, Cmd msg) | |
update message model = | |
case message of | |
FormMsg msg -> | |
Form.update msg model ! [] | |
-- _ -> model ! [] | |
view : Model -> Html Msg | |
view model = | |
div [ style [("padding", "15px")] ] | |
[ Input.selectInput optionsList (Form.getFieldAsString "Select" model) [] | |
|> Html.map FormMsg | |
, br [] [] | |
, Input.textInput (getFieldAsString "Count" model) [] | |
|> Html.map FormMsg | |
, Input.dumpErrors model |> Html.map FormMsg | |
, text <| toString model | |
, div [] | |
[ text "getOutput " | |
, text <| toString <| getOutput model | |
] | |
] | |
-- VALIDATION | |
validation : Validation CustomError FormItems | |
validation = | |
V.form2 FormItems | |
(V.get "Select" V.string) | |
valCount | |
-- (V.oneOf [ valCount, valCount2 ]) | |
valCount : Validation CustomError Float | |
valCount = | |
V.get "Select" V.string | |
`V.andThen` (\binding -> | |
case binding of | |
"option2" -> | |
V.get "Count" V.float | |
`V.andThen` (\tpc -> | |
case round tpc `rem` 4 == 0 of | |
True -> | |
valFloat "Count" -- checks > 0 | |
_ -> | |
let _ = Debug.log "failing" tpc | |
in | |
V.fail InvalidFloat) | |
_ -> | |
valFloat "Count") | |
-- valCount2 : Validation CustomError Float | |
-- valCount2 = | |
-- V.get "Select" V.emptyString | |
-- `V.andThen` \_ -> valFloat "Count" | |
valFloat : String -> Validation CustomError Float | |
valFloat fieldName = | |
V.get fieldName V.float `V.andThen` (V.minFloat 0) | |
optionsList = | |
["option1", "option2", "option3"] | |
|> L.map (\x -> (x,x)) | |
|> (::) ("", "Select") | |
-- | |
main = | |
Html.program | |
{ init = (init, Cmd.none) | |
, update = update | |
, view = view | |
, subscriptions = always Sub.none | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment