Last active
October 22, 2016 11:04
-
-
Save nmk/e9fd97dd1a62c04fc52545fec5a9b4ef to your computer and use it in GitHub Desktop.
Dependent fields validation with elm-simple-form
This file contains 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 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 "" | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment