-
Notifications
You must be signed in to change notification settings - Fork 34
Form fields for union types #49
Comments
Gosh, I'm afraid I don't have any satisfying answer for this, sorry... Not possible to do dependant validations at the moment. |
I did something like this: type alias CompanyName =
String
type AccountType
= Private
| Business CompanyName
type alias Registration =
{ email : String
, accountType : AccountType
}
validateRegistration : Validation () Registration
validateRegistration =
let
company =
"company" := oneOf [ string, emptyString ]
in
Validate.succeed Registration
|: ("email" := email)
|: (("accountType" := validateAccountType) |: company)
validateAccountType : Validation e (String -> AccountType)
validateAccountType =
string
|> map
(\s ->
case s of
"business" ->
\x -> Business x
_ ->
\_ -> Private
) This works but is not ideal, as it allows |
Okey, you could try using |
This is the first thing I tried. However if the validation is like validateRegistration : Validation () Registration
validateRegistration =
Validate.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 then |
Okey, that's not intended, as fair as I can remember. |
I made an example: https://gist.github.com/nmk/e9fd97dd1a62c04fc52545fec5a9b4ef |
Thanks for the example. I'm writing tests first as this change will break things. |
Oh... could you try adding parenthesis like this: (("accountType" := string) `andThen` accountType) otherwise |
You are right! That works and solves the problem nicely. It would be nice if we could get this in the documentation. I will try to get a PR in next week. |
Just for completeness, here is a link to a working gist: https://gist.github.com/nmk/e9fd97dd1a62c04fc52545fec5a9b4ef |
Assume one needs form fields for the following type
Account
:I assume the correct form for the UI would be a radio selection between
Private
andBusiness
, with a text input for the company name appearing ifBusiness
is selected.What would the correct approach be with elm-simple-form?
The part which is unclear to me is how to set up the validation for have one field depend on another.
The text was updated successfully, but these errors were encountered: