Get updates via , Github, or RSS | About

Episode 42: Form Validation Jul 20, 2017

Form validation doesn’t need to be difficult or require a significant amount of code. Using elm-validate will simplify your validation and increase code reuse throughout your application.

Examples

View.elm

validate : Model -> List String
validate model =
    Validate.all
        [ .password >> Validate.ifBlank "Password cannot be blank"
        , .password >> ifLengthLessThan 8 "Password must be 8+ characters"
        , .email >> Validate.ifInvalidEmail "Email address is invalid"
        , .age >> Validate.ifNotInt "Age must be a number"
        ]
        model


ifLengthLessThan : Int -> error -> Validate.Validator error String
ifLengthLessThan minLength err =
    Validate.ifInvalid
        (\string ->
            (String.length string) < minLength
        )
        err