0

我有一个表单,用户可以在其中从预先存在的值列表中进行选择,或者创建一个新的(文本)值。

最优雅的编码方式是什么?

我最好的(和 IMO 不那么优雅)这样做的方法是定义:

data MyInput = MyInput {createNew :: Bool, newVal :: Maybe Text, existingVal :: Maybe Text}

myForm :: [(Text,Text)] -> Html -> MForm MySite MySite (FormResult MyInput,Widget)
myForm exisingVals = renderTable $ MyInput
    <$> areq boolField "Create new" (Just False)
    <*> aopt textField "New val" Nothing
    <*> aopt (selectField existingVals) "Existing values" Nothing

收到表单后,通过以下方式传递结果:

getMyValue :: MyInput -> Either ErrorMsg Text
getMyValue i = if createNew i
                   then if newVal i == Nothing || existingVal i /= Nothing
                            then Left "Missing new value or illegal input"
                            else Right . fromJust . newVal $ i
                   else if existingVal i == Nothing || newVal i /= Nothing
                            then Left "Missing selection or illegal input"
                            else Right . fromJust . existingVal $ i

并让处理程序决定是重新发送表单,还是根据结果继续。

有什么更好/更短的建议吗?我的真实表单有两个这样的选择/创建字段,这使得数据结构和处理更加繁琐。谢谢,

4

1 回答 1

0

您可以分解出通用代码,使用模式匹配和守卫,并使用接受访问器函数的高阶函数进行泛化:

import Control.Arrow ((&&&))

getVal isNew newVal oldVal i | isNew i   = checkVal "new value" $ (newVal &&& oldVal) i
                             | otherwise = checkVal "selection" $ (oldVal &&& newVal) i
  where checkVal _ (Just val, Nothing) = Right val
        checkVal name _                = Left $ "Missing " ++ name ++ " or illegal input"

getMyVal = getVal createNew newVal existingVal
于 2012-02-08T17:27:24.053 回答