Hello!

Disclaimer : I am not a programmer and don't understand computer science 
terms. The only formal education that I have had with programming is BASIC 
(school) and C (college - can't remember most of it :-p). Javascript seems 
clumsy and I came across Elm and tried learning it. Was working through the 
Elm tutorial and tried adding some of the extra suggested stuff in the 
"Forms" section of the Tutorial to test my own understanding.

Tried compiling the following code. Idea is to validate form on pressing 
the Submit button.

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput, onClick)
import String

main =
  Html.beginnerProgram { model = model, view = view, update = update }

-- MODEL

type alias Model =
  { name : String
  , age : String
  , password : String
  , passwordAgain : String
  , flag : Bool
  }

model : Model
model =
  Model "" "" "" "" False

-- UPDATE

type Msg
    = Name String
    | Password String
    | PasswordAgain String
    | Age String
    | SetFlag

update : Msg -> Model -> Model
update msg model =
  case msg of
    Name name ->
      { model | name = name }

    Age age ->
      { model | age = age}

    Password password ->
      { model | password = password }

    PasswordAgain password ->
      { model | passwordAgain = password }
      
    SetFlag ->
      { model | flag = True }

-- VIEW

view : Model -> Html Msg
view model =
  div []
    [ input [ type_ "text", placeholder "Name", onInput Name ] []
    , input [ type_ "text", placeholder "Age", onInput Age ] []
    , input [ type_ "password", placeholder "Password", onInput Password ] 
[]
    , input [ type_ "password", placeholder "Re-enter Password", onInput 
PasswordAgain ] []
    , div [] [button [ onClick SetFlag ] [text "Submit"] ]
    , viewValidation model
    ]

viewValidation : Model -> Html msg
viewValidation model =
  if model.flag == True then
    { model | flag = False }
    let (color, message) =
      if Result.withDefault 0 (String.toInt model.age) == 0 then
          ("red", "Funny Age")
        else if String.length model.password < 9 then
          ("red", "Password too short")
        else if model.password == model.passwordAgain then
          ("green", "OK")
        else
          ("red", "Passwords do not match!")
    in
      div [ style [("color", color)] ] [ text message ] 

This throws a compiler error as

Detected errors in 1 module. 

-- SYNTAX PROBLEM 
-------------------------------------------------------------- 

I ran into something unexpected when parsing your code! 

68| let (color, message) =
 ^ 
I am looking for one of the following things:  

an 'else' branch

 

whitespace 




I apologise in advance if this is not the correct platform to ask this or 
if Elm is not supposed to be for novices, but I would greatly appreciate 
any inputs regarding this.

Best Regards
Anurup

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to