How do I use digestive functors to create a form that has a programmatically generated list of checkboxes, which would return a list. For example:
[x] Milk
[ ] Cereals
[x] Ground meat
would return ["Milk", "Ground meat"]
.
I'm expecting the type would be something like:
form :: (Functor m, Monad m) => [String] -> HappstackForm m Html BlazeFormHtml [String]
There is no standard way to do so, but
digestive-functors
is highly composable using theApplicative
interface, so you can easily create what you want.You can define a
checkBox
which returns aMaybe String
, i.e. the name of the element if it was checked.You can then loop over a list of strings to create a checkbox like this for each element in the list:
The
catMaybes :: [Maybe a] -> [a]
helps you to reduce the result further:And finally, we can instantiate the actual form: