[Haskell-cafe] dynamic web form generation for algebraic data types
Olaf Klinke
olf at aatal-apotheke.de
Thu Mar 2 10:54:22 UTC 2023
Dear Cafe,
has anyone ever attempted (and maybe succeeded) in building dynamic
forms using one of the Haskell web frameworks (Yesod,Servant,...)?
By "dynamic" form I mean a form that changes the number of fields based
on selections the user makes in other fields of the form.
For example, say one has an algebraic data type
data T = Number Int | Check Bool T
A form for such a type would initially consist of radio buttons or a
drop-down list with options "Number" and "Check" that lets the user
select the constructor. When "Number" is selected, an <input
type="number"> field is shown. When "Check" is selected, an <input
type="checkbox"> is displayed next to another form of this kind.
In the end, one would use the GHC.Generics machinery to generate forms
for a wide range of algebraic data types. I've seen this in the Clean
language's iTask library [1] and it's very convenient.
Of course this would involve a lot of JavaScript like
document.createElement() as well as book-keeping how to re-asseble the
fields into a T value upon submission. At least the latter is already
handled by libraries such as yesod-form.
Olaf
[1] https://cloogle.org/src/#itasks/iTasks/UI/Editor/Generic
[2] https://github.com/haskell-servant/servant-swagger/issues/80
More information about the Haskell-Cafe
mailing list