[Haskell-cafe] Avoiding ad-hoc with yesod-form

Yotam Ohad yotam2206 at gmail.com
Mon Jul 24 10:54:33 UTC 2017


Hello cafe,

I have a custom data type:
data Foo = Foo | Bar | Baz
    deriving (Show, Bounded, Enum)
In my yesod app I need to have two forms, one that get two Foo and a second
that recieve three Foo. From What I've seen online I need to create two new
data types:
data Foo2 = { foo1 :: Foo, foo2 :: Foo }
data Foo3 = { foo1 :: Foo, foo2 :: Foo, foo3 :: Foo }

This is tedious and very much ad-hoc. In my actual app it goes up to Foo6
:( Is there a better way to do it?

This is how I defined the forms:
foo2AForm :: AForm Handler Foo2
foo2AForm = Foo2
    <$> areq (selectFieldList foos) "foo1" Nothing
    <*> areq (selectFieldList foos) "foo2" Nothing
  where
    foos:: [(Text, Role)]
    foos= map (pack . show &&& id) [minBound..]

foo2Form :: Html -> MForm Handler (FormResult Foo2, Widget)
foo2Form = renderTable teamAForm

foo3AForm :: AForm Handler Foo3
foo3AForm = Foo3
    <$> areq (selectFieldList foos) "foo1" Nothing
    <*> areq (selectFieldList foos) "foo2" Nothing
    <*> areq (selectFieldList foos) "foo3" Nothing
  where
    foos:: [(Text, Role)]
    foos= map (pack . show &&& id) [minBound..]

foo3Form :: Html -> MForm Handler (FormResult Foo2, Widget)
foo3Form = renderTable teamAForm

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170724/7ec0c07f/attachment.html>


More information about the Haskell-Cafe mailing list