diff --git a/src/Form/Validation.elm b/src/Form/Validation.elm index 856e7e94..38a25c4b 100644 --- a/src/Form/Validation.elm +++ b/src/Form/Validation.elm @@ -1,7 +1,7 @@ module Form.Validation exposing ( Validation, andMap, andThen, fail, fromMaybe, fromResult, map, map2, parseWithError, succeed, withError, withErrorIf, withFallback , value - , fail2, fieldName, withError2 + , fail2, fieldName, withError2, withErrorIf2 ) {-| @@ -97,6 +97,21 @@ withErrorIf includeError (Validation _ key _) error (Validation viewField name ( ) +{-| -} +withErrorIf2 : Bool -> Validation error ignored field -> error -> Validation error parsed named -> Validation error parsed named +withErrorIf2 includeError (Validation _ key _) error (Validation viewField name ( maybeParsedA, errorsA )) = + -- TODO use something like { field : kind } for type variable to check that it represents a field + Validation viewField + name + ( maybeParsedA + , if includeError then + errorsA |> insertIfNonempty (key |> Maybe.withDefault "") [ error ] + + else + errorsA + ) + + --map : (parsed -> mapped) -> Validation error parsed named -> Validation error mapped named diff --git a/tests/FormTests.elm b/tests/FormTests.elm index 7b655342..5e848e69 100644 --- a/tests/FormTests.elm +++ b/tests/FormTests.elm @@ -23,26 +23,28 @@ all = describe "Form Parser" <| let passwordConfirmationParser = - Form.init + Form.init2 (\password passwordConfirmation -> - Validation.succeed - (\passwordValue passwordConfirmationValue -> - Validation.succeed { password = passwordValue } - |> Validation.withErrorIf (passwordValue /= passwordConfirmationValue) - passwordConfirmation - "Must match password" - ) - |> Validation.andMap password - |> Validation.andMap passwordConfirmation - |> Validation.andThen identity + { combine = + Validation.succeed + (\passwordValue passwordConfirmationValue -> + Validation.succeed { password = passwordValue } + |> Validation.withErrorIf2 (passwordValue /= passwordConfirmationValue) + passwordConfirmation + "Must match password" + ) + |> Validation.andMap password + |> Validation.andMap passwordConfirmation + |> Validation.andThen identity + , view = \info -> Div + } ) - (\_ _ _ -> Div) - |> Form.field "password" (Field.text |> Field.required "Password is required") - |> Form.field "password-confirmation" (Field.text |> Field.required "Password confirmation is required") + |> Form.field2 "password" (Field.text |> Field.required "Password is required") + |> Form.field2 "password-confirmation" (Field.text |> Field.required "Password confirmation is required") in [ test "matching password" <| \() -> - Form.runServerSide + Form.runServerSide4 (fields [ ( "password", "mypassword" ) , ( "password-confirmation", "mypassword" ) @@ -55,7 +57,7 @@ all = ) , test "non-matching password" <| \() -> - Form.runServerSide + Form.runServerSide4 (fields [ ( "password", "mypassword" ) , ( "password-confirmation", "doesnt-match-password" )