Skip to content

Instantly share code, notes, and snippets.

@shinkwhek
Created September 15, 2019 16:21
Show Gist options
  • Save shinkwhek/d72fd640ddafaca86d49b0882f9bc0f8 to your computer and use it in GitHub Desktop.
Save shinkwhek/d72fd640ddafaca86d49b0882f9bc0f8 to your computer and use it in GitHub Desktop.
form light
-- Input a user name and password. Make sure the password matches.
--
-- Read how it works:
-- https://guide.elm-lang.org/architecture/forms.html
--
import Browser
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput, onClick)
-- MAIN
main =
Browser.sandbox { init = init, update = update, view = view }
-- MODEL
type alias Model =
{ name : String
, password : String
, passwordAgain : String
, submit : Bool
}
init : Model
init =
Model "" "" "" False
-- UPDATE
type Msg
= Name String
| Password String
| PasswordAgain String
| Submit
update : Msg -> Model -> Model
update msg model =
case msg of
Name name ->
{ model | name = name }
Password password ->
{ model | password = password }
PasswordAgain password ->
{ model | passwordAgain = password }
Submit ->
{ model | submit = True }
-- VIEW
view : Model -> Html Msg
view model =
div []
[ viewInput "text" "Name" model.name Name
, viewInput "password" "Password" model.password Password
, viewInput "password" "Re-enter Password" model.passwordAgain PasswordAgain
, isSubmit viewValidation model
, div [] [button [ onClick Submit ] [ text "submit" ]]
]
isSubmit : (Model -> Html msg) -> Model -> Html msg
isSubmit f model =
if model.submit then
f model
else
div [] []
viewInput : String -> String -> String -> (String -> msg) -> Html msg
viewInput t p v toMsg =
input [ type_ t, placeholder p, value v, onInput toMsg ] []
viewValidation : Model -> Html msg
viewValidation model =
if model.password == model.passwordAgain then
div [ style "color" "green" ] [ text "OK" ]
else
div [ style "color" "red" ] [ text "Passwords do not match!" ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment