Trigger warning: I hate form libraries. I really do. I’ve been fighting with them for as long as I can remember (mostly with the Django framework). As soon as I want to build something a bit custom, I would scratch my head, cry in despair, close my computer, put it to fire, and promise myself to never try to build custom forms ever again.
But since forms are central to the web, and since I love Elm so much I thought I would find a way to build custom forms quite easily. Until now I’ve been creating forms “from scratch”: one view function and one Msg per field, and a parsing function, you know the drill. But this ended up being very repetitive, so I started searching for packages, hoping to find something that would allow me to:
- Parse the form input and get the data in a specific type, or show errors in the form
- Have full control on the rendering of the form and the widgets
One of the packages I found is composable-form, which looked really nice (I thought the composition part was very clever, as well as the parsing part).
Unfortunately, as soon as you need custom widgets or specific rendering (eg. if you need to change the way the form is rendered, or add classes to input fields), things seem to get messy (or maybe I missed something): you need to copy-paste whole parts of the package, understand the internals, and adapt them. To help you understand why I couldn’t get this package work for my usecase, here’s what I need to do: my forms are not just one block of fields, but are split into sections, which are not just a title and some fields, but blocks with a title, a description and possibly other Html
elements. And “submit” buttons sometimes have other buttons with other actions, or just a “Cancel” button to close a modal.
I know there are other packages out there, such as elm-form-decoder 1.4.0 (which requires one error variant for each error for each field, and doesn’t come with any rendering mechanism).
I could continue rolling my own repetitive form code, just like I could write my own JSON decoding library, but hopefully the one in Elm allows me to do everything I need, which is not the case with forms. I’m surprised I couldn’t find a package that allows customizing the rendering/widgets without copying all the package code. Maybe that’s just not possible (I’ve tried to come up with a generic proof of concept but didn’t manage to), and the best way to deal with this is to roll your own form code? Or maybe that’s possible and I’m missing something?
Anyway, I’m very curious about the way you folks deal with forms in your projects, and I’d be happy to read it!