Try this code if you want to see how elm behave in the console:
This file contains hidden or 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 Main exposing (..) | |
import Html exposing (..) | |
import Html.Attributes exposing (..) | |
import Html.Events exposing (onInput) | |
-- import Debug exposing (..) | |
main = | |
Html.beginnerProgram | |
{ model = model | |
, view = view | |
, update = update | |
} | |
-- MODEL | |
type alias Model = | |
{ name : String | |
, password : String | |
, passwordAgain : String | |
} | |
model : Model | |
model = | |
Model "" "" "" | |
-- UPDATE | |
type Msg | |
= Name String | |
| Password String | |
| PasswordAgain String | |
update : Msg -> Model -> Model | |
update msg model = | |
let | |
_ = Debug.log "msg" msg | |
_ = Debug.log "model before update" model | |
in | |
case msg of | |
Name name -> | |
{ model | name = name } | |
Password password -> | |
{ model | password = password } | |
PasswordAgain password -> | |
{ model | passwordAgain = password } | |
-- VIEW | |
view : Model -> Html Msg | |
view model = | |
let | |
_ = Debug.log "model to view" model | |
in | |
div [] | |
[ input [ type_ "text", placeholder "Name", onInput Name ] [] | |
, input [ type_ "password", placeholder "Password", onInput Password ] [] | |
, input [ type_ "password", placeholder "Re-enter Password", onInput PasswordAgain ] [] | |
, viewValidation model | |
] | |
viewValidation : Model -> Html msg | |
viewValidation model = | |
let | |
(color, message) = | |
if model.password == model.passwordAgain then | |
("green", "OK") | |
else | |
("red", "Passwords do not match!") | |
in | |
div [ style [("color", color)] ] [ text message ] |
No comments :
Post a Comment