Advertisement
Guest User

Untitled

a guest
Apr 8th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. module Main exposing (..)
  2.  
  3. import Html exposing (..)
  4. import Html.Attributes exposing (..)
  5. import Html.Events exposing (..)
  6. import Form exposing (Form)
  7. import Form.Validate as Validate exposing (..)
  8. import Form.Input as Input
  9.  
  10.  
  11. type alias User =
  12. { name : String
  13. , password : String
  14. , passwordConfirmation : String
  15. }
  16.  
  17.  
  18. type alias Model =
  19. { form : Form String User }
  20.  
  21.  
  22. type Msg
  23. = NoOp
  24. | FormMsg Form.Msg
  25.  
  26.  
  27. init : ( Model, Cmd Msg )
  28. init =
  29. ( { form = Form.initial [] validate }, Cmd.none )
  30.  
  31.  
  32. validate : Validation String User
  33. validate =
  34. succeed User
  35. |> andMap (field "name" (string |> withCustomError "Input Name !"))
  36. |> andMap (field "password" (string |> withCustomError "Input Password!"))
  37. |> andMap ((field "password" string) |> andThen validateConfirmation)
  38.  
  39.  
  40. validateConfirmation : String -> Validation String String
  41. validateConfirmation password =
  42. field "passwordConfirmation"
  43. (string
  44. |> andThen
  45. (\confirmation ->
  46. if password == confirmation then
  47. succeed confirmation
  48. else
  49. fail (customError "Confirmation Error")
  50. )
  51. )
  52.  
  53.  
  54.  
  55. -- Forward form msgs to Form.update
  56.  
  57.  
  58. update : Msg -> Model -> ( Model, Cmd Msg )
  59. update msg ({ form } as model) =
  60. case msg of
  61. NoOp ->
  62. ( model, Cmd.none )
  63.  
  64. FormMsg formMsg ->
  65. ( { model | form = Form.update validate formMsg form }, Cmd.none )
  66.  
  67.  
  68.  
  69. -- Render form with Input helpers
  70.  
  71.  
  72. view : Model -> Html Msg
  73. view { form } =
  74. Html.map FormMsg (formView form)
  75.  
  76.  
  77. formView : Form String User -> Html Form.Msg
  78. formView form =
  79. let
  80. -- error presenter
  81. errorFor field =
  82. case field.liveError of
  83. Just error ->
  84. -- replace toString with your own translations
  85. div [ class "error" ] [ text (toString error) ]
  86.  
  87. Nothing ->
  88. text ""
  89.  
  90. -- fields states
  91. name =
  92. Form.getFieldAsString "name" form
  93.  
  94. password =
  95. Form.getFieldAsString "password" form
  96.  
  97. passwordConfirmation =
  98. Form.getFieldAsString "passwordConfirmation" form
  99. in
  100. div []
  101. [ div []
  102. [ label [] [ text "Name" ]
  103. , Input.textInput name []
  104. , errorFor name
  105. ]
  106. , div []
  107. [ label [] [ text "Password" ]
  108. , Input.textInput password []
  109. , errorFor password
  110. ]
  111. , div []
  112. [ label [] [ text "Password Confirmation" ]
  113. , Input.textInput passwordConfirmation []
  114. , errorFor passwordConfirmation
  115. ]
  116. , button
  117. [ onClick Form.Submit ]
  118. [ text "Submit" ]
  119. ]
  120.  
  121.  
  122. main =
  123. Html.program
  124. { init = init
  125. , update = update
  126. , view = view
  127. , subscriptions = \_ -> Sub.none
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement