Skip to content

Instantly share code, notes, and snippets.

@nmk
Last active October 22, 2016 11:04
Show Gist options
  • Select an option

  • Save nmk/e9fd97dd1a62c04fc52545fec5a9b4ef to your computer and use it in GitHub Desktop.

Select an option

Save nmk/e9fd97dd1a62c04fc52545fec5a9b4ef to your computer and use it in GitHub Desktop.

Revisions

  1. nmk revised this gist Oct 22, 2016. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions Main.elm
    Original file line number Diff line number Diff line change
    @@ -12,14 +12,14 @@ type alias CompanyName =
    String


    type AccountType
    type Account
    = Private
    | Business CompanyName


    type alias Registration =
    { email : String
    , accountType : AccountType
    , account : Account
    }


    @@ -71,11 +71,11 @@ validateRegistration : Validation () Registration
    validateRegistration =
    succeed Registration
    |: ("email" := email)
    |: (("accountType" := string) `andThen` accountType)
    |: (("accountType" := string) `andThen` validateAccount)


    accountType : String -> Validation e AccountType
    accountType s =
    validateAccount : String -> Validation e Account
    validateAccount s =
    case s of
    "business" ->
    succeed Business |: ("companyName" := string)
  2. nmk revised this gist Oct 22, 2016. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions Main.elm
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,13 @@ import Html.App as Html
    import Html.Attributes exposing (..)


    type alias CompanyName =
    String


    type AccountType
    = Private
    | Business String
    | Business CompanyName


    type alias Registration =
    @@ -27,6 +31,7 @@ type Msg
    = RegFormMsg Form.Msg


    main : Program Never
    main =
    Html.beginnerProgram
    { model = { registrationForm = Form.initial [] validateRegistration }
    @@ -66,7 +71,7 @@ validateRegistration : Validation () Registration
    validateRegistration =
    succeed Registration
    |: ("email" := email)
    |: ("accountType" := string `andThen` accountType)
    |: (("accountType" := string) `andThen` accountType)


    accountType : String -> Validation e AccountType
  3. nmk created this gist Oct 20, 2016.
    122 changes: 122 additions & 0 deletions Main.elm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,122 @@
    module Main exposing (..)

    import Form exposing (Form, FieldState)
    import Form.Input as Input
    import Form.Validate exposing (..)
    import Html exposing (..)
    import Html.App as Html
    import Html.Attributes exposing (..)


    type AccountType
    = Private
    | Business String


    type alias Registration =
    { email : String
    , accountType : AccountType
    }


    type alias Model =
    { registrationForm : Form () Registration }


    type Msg
    = RegFormMsg Form.Msg


    main =
    Html.beginnerProgram
    { model = { registrationForm = Form.initial [] validateRegistration }
    , view = view
    , update = update
    }


    view : Model -> Html Msg
    view { registrationForm } =
    Html.map RegFormMsg (viewForm registrationForm)


    update : Msg -> Model -> Model
    update msg model =
    case msg of
    RegFormMsg msg ->
    { model | registrationForm = Form.update msg model.registrationForm }



    ----------------------------


    (:=) : String -> Validation e a -> Validation e a
    (:=) =
    get
    infixl 7 :=


    (|:) : Validation e (a -> b) -> Validation e a -> Validation e b
    (|:) =
    apply


    validateRegistration : Validation () Registration
    validateRegistration =
    succeed Registration
    |: ("email" := email)
    |: ("accountType" := string `andThen` accountType)


    accountType : String -> Validation e AccountType
    accountType s =
    case s of
    "business" ->
    succeed Business |: ("companyName" := string)

    _ ->
    succeed Private


    radioGroup : List ( String, String ) -> FieldState () String -> Html Form.Msg
    radioGroup options state =
    let
    item ( v, l ) =
    label [] [ Input.radioInput v state [ value v ], text l ]
    in
    p [] (List.map item options)


    viewForm : Form () Registration -> Html Form.Msg
    viewForm form =
    let
    gS =
    flip Form.getFieldAsString form

    isBusiness =
    (gS "accountType").value == Just "business"
    in
    Html.form []
    [ pre [] [ text (toString (Form.getOutput form)) ]
    , Input.dumpErrors form
    , p []
    [ label
    []
    [ text "Email: "
    , Input.textInput (gS "email") []
    ]
    ]
    , radioGroup
    [ ( "private", "Private" )
    , ( "business", "Business" )
    ]
    (gS "accountType")
    , if isBusiness then
    p []
    [ label [] [ text "Company name:" ]
    , Input.textInput (gS "companyName") []
    ]
    else
    text ""
    ]