Discusses the problems with form validation in Elm and presents a solution for handling form validation in a pleasant way using the Form Validator extension.
Elm Form ValidationPresented by Brooke Kuhlmann
View Slide
Form Examplehttps://www.alchemists.io/projects/form-validator
Informative Errorshttps://www.alchemists.io/projects/form-validator
The Problem• Pages that deal with too much data• Repeating the same pattern over multiple forms/pages.• Repeating common validation strategies.
Model Example (single form)type alias Model ={username: String,email: String,password: String,role: String,notes: String}ℹ Gets worse when you need to model data other than your form.
Model Example (multiple forms)type alias Model ={userForm = UserForm,addressForm = AddressForm}type alias UserForm ={username: String,password: String}type alias AddressForm ={line1: String,country: String,state: String,zip: String}⚠ Unwieldy!
View ExampleviewUsernameField : Model -> Html MessageviewUsernameField model =div [class "field"] [label [for "username", class "label"] [text "Username"],input [name "username",type_ "text",class "input",value <| model.username,onInput <| UsernameInputChange,onBlur <| UsernameInputBlur] []]ℹ Necessary regardless of solution.
Message Exampletype Message= UsernameInputChange String| UsernameInputBlur String| PasswordInputChange String| PasswordInputBlue String| EmailInputBlur String| EmailInputBlur String| Save⚠ Unwieldy with lots of duplication.
Update Exampleupdate : Message -> Model -> (Model, Cmd Message)update message model =case message ofUsernameInputChange username ->({model | username = username}, Cmd.none)UsernameInputBlur username ->({model | username = username}, Cmd.none)-- Pattern repeats for other branchesSave ->letisInvalid = String.isBlank model.username || ...inif isInvalid then({model | formError = Just "Errors detected!"}, Cmd.none)else({model | formError = Nothing}, Cmd.none)ℹ Doesn't begin to cover individual errors.
A SolutionUse a reusable component!https://www.alchemists.io/projects/form-validator
Form ValidatorWalkthrough demo.https://www.alchemists.io/projects/form-validator
Thanks!https://www.alchemists.io
Thanks!🙇https://www.alchemists.io