Form Decoding: the next era of the Form Validation

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

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

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

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

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

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

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

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