嵌套表单导致Haskell

时间:2015-07-21 23:18:02

标签: haskell yesod applicative yesod-forms

我有以下处理程序/模板组合:

处理程序/ automation.hs

data AutomationRequest = AutomationRequest {
    arEnabled :: Bool
  , arTemplate :: Text
  , arSchedules :: Textarea
}

getAutomationR :: Handler Html
getAutomationR = do
    (formWidget, formEnctype) <- generateFormPost form
    defaultLayout $(widgetFile "automation")

form :: Form AutomationRequest
form extra = do
    (enabledRes, enabledView) <- mreq checkBoxField "" Nothing
    (templateRes, templateView) <- mreq textField (withPlaceholder "..." $ bfs (""::Text)) Nothing
    (schedulesRes, schedulesView) <- mreq textareaField (withPlaceholder "..." $ bfs (""::Text)) Nothing
    (_, submitView) <- mbootstrapSubmit $ BootstrapSubmit ("Save"::Text) ("btn-primary"::Text) []
    let requestRes = AutomationRequest <$> enabledRes <*> templateRes <*> schedulesRes
        widget = $(widgetFile "automation-form")
    return (requestRes, widget)

模板/ automation.hamlet

<form method=post role=form action=@{AutomationR} enctype=#{formEnctype}>
    ^{formWidget}

模板/自动化form.hamlet

#{extra}

<div .panel .panel-default>
    <div .panel-heading>^{fvInput enabledView} ...
    <div .panel-body>
        ^{fvInput templateView}
        ^{fvInput schedulesView}

^{fvInput submitView}

这可以按预期工作,但我想要其他功能:

a)我希望能够嵌套数据结构,如:

data AutomationRequestCollection = AutomationRequestCollection {
    arcItemAbc :: AutomationRequest
  , arcItemDef :: AutomationRequest
  ... -- 10 Items

}

data AutomationRequest = AutomationRequest {
    arEnabled :: Bool
  , arTemplate :: Text
  , arSchedules :: Textarea
}

我不知道如何将嵌套应用到let requestRes = AutomationRequest <$> enabledRes <*> templateRes <*> schedulesRes

b)重复使用itemAbc,itemDef,...:

的HTML面板
-- loop somehow
<div .panel .panel-default>
    <div .panel-heading>^{fvInput enabledView} ...
    <div .panel-body>
        ^{fvInput templateView}
        ^{fvInput schedulesView}

任何可以推动我走向正确方向的想法?

1 个答案:

答案 0 :(得分:1)

我不确定(b),但(a)应该是直截了当的申请组成,例如:

Foo <$> (Bar <$> baz <*> bin) <*> qux

也可以更容易看出是否将其分解为多个功能:

bar = Bar <$> baz <*> bin
foo = Foo <$> bar <*> qux