gpt4 book ai didi

haskell - Haskell/Yesod 中的分组复选框

转载 作者:行者123 更新时间:2023-12-04 15:23:07 26 4
gpt4 key购买 nike

我目前正在 Yesod Web 平台上使用 Haskell 进行开发,我需要在我的一个网页中创建一个表单。此表单必须包含多个复选框,但需要将它们分组。例如,用户需要在 10 个复选框、5 个项目的列表中选择 3 个学科,其中最多 2 个可以是武器。

我已经在 Yesod 网站上阅读了本书的部分内容,据我了解,我需要创建一个自定义字段来创建分组复选框。不幸的是,我对 Haskell 不是很好(1-2 周前才开始使用它),我在理解如何实现这一点时遇到了一些困难。作为引用,这是我在 Yesod 网站上阅读的关于自定义字段的书的页面:http://www.yesodweb.com/book/forms

知道了这一点,我的问题是:如何在 Haskell/Yesod 中创建分组复选框?

编辑:在我提供的链接中,我了解 Field 构造函数的最后两个值(fieldView 和 fieldEnctype),它是第一个值(fieldParse),我不知道如何为我的目的进行修改。这是我引用的链接中的示例:

passwordConfirmField :: Field Handler Text
passwordConfirmField = Field
{ fieldParse = \rawVals _fileVals ->
case rawVals of
[a, b]
| a == b -> return $ Right $ Just a
| otherwise -> return $ Left "Passwords don't match"
[] -> return $ Right Nothing
_ -> return $ Left "You must enter two values"
, fieldView = \idAttr nameAttr otherAttrs eResult isReq ->
[whamlet|
<input id=#{idAttr} name=#{nameAttr} *{otherAttrs} type=password>
<div>Confirm:
<input id=#{idAttr}-confirm name=#{nameAttr} *{otherAttrs} type=password>
|]
, fieldEnctype = UrlEncoded
}

最佳答案

因此,经过大量研究,我意识到在我的情况下,为了返回被选中的复选框的值,我需要做:

fieldParse  = \rawVals _fileVals ->
return $ Right $ Just rawVals

由于 rawVals 包含已检查复选框的所有值的 Text 列表。

关于haskell - Haskell/Yesod 中的分组复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19869932/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com