Form Decoding: the next era of the Form Validation

#1

I’ve published a post about form decoding, a sort of form validation especially suitable for statically typed languages like Elm, and released form decoding library elm-form-decoder.

15 Likes
#2

This is pretty cool!

It reminds me quite a bit of stoeffel/elm-verify which is conceptually similar: turn one datastructure into another, while collecting errors when things go wrong.

Nice work :+1:

2 Likes
#3

Thanks very much for reading and also valuable information.
I’ll add “Related work” section on package README, and reffer it.

#4

This looks great! I wonder if it is possible to validate fields independently of each other. What would the model look like?

My use case is validating a field as soon as the focus leaves the input.

2 Likes
#5

Thanks for interested in.
Is your use case differ from my real world example?
(It shows error immediately if you input some invalid value like “foo” in age field and leave focus.)

1 Like
#6

Oh! It actually works as expected. Somehow I switched the focus without typing anything into the field, and expected an error because the field was required. Then I assumed that there was no validation on blur.

1 Like
#7

Okay, so only you have to do is just running decoders for fields:

ageError : RegistrationForm -> Html msg
ageError form =
  div [ class "error-field" ]
    <| List.map (\err -> p [ class "error-text" ] [ text err ]) <| Decoder.errors age_ form

FYI, my sample form does not warn till pressing register button for EFO, but you could warn required field when blur by handling blur event.

1 Like
closed #8

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.