【问题标题】:Nested form result in HaskellHaskell中的嵌套表单结果
【发布时间】:2015-07-22 21:27:47
【问题描述】:

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

handler/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)

templates/automation.hamlet

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

templates/automation-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 &lt;$&gt; enabledRes &lt;*&gt; templateRes &lt;*&gt; schedulesRes

b) 为 itemAbc、itemDef、...重用 HTML 面板:

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

有什么想法可以将我推向正确的方向吗?

【问题讨论】:

    标签: haskell yesod applicative yesod-forms


    【解决方案1】:

    我不确定(b),但(a)应该是简单的应用组合,例如:

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

    如果您将其分解为多个函数,也可以更容易地查看:

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

    【讨论】:

    • 酷。谢谢。以某种方式可以使用具有应用风格的记录语法吗? AutomationRequestCollection { arcItemAbc = ... , arcItemDef = ...}
    猜你喜欢
    • 2022-01-20
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    相关资源
    最近更新 更多