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 alias CompanyName = String type Account = Private | Business CompanyName type alias Registration = { email : String , account : Account } type alias Model = { registrationForm : Form () Registration } type Msg = RegFormMsg Form.Msg main : Program Never 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` validateAccount) validateAccount : String -> Validation e Account validateAccount 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 "" ]