消化函数和 listOf

Digestive-functors and listOf

我已经能够在 snap 应用程序中成功地使用 digestive-functors 和简单的表单;但是,我在尝试处理隐藏输入列表时遇到了困难。我不太了解如何使用 listOf。谁能指出我正确的方向?谢谢。

上一页呈现的 html 包含以下隐藏字段:

<div id='messageForm.recipients' class='inputList'>
  <input type='hidden' name='messageForm.recipients.indices' value='0' />
  <input type='hidden' id='messageForm.recipients.-1.email' name='messageForm.recipients.-1.email' value />
  <input type='hidden' id='messageForm.recipients.0.email' name='messageForm.recipients.0.email' value='emai1l@email.com' /> 
  <input type='hidden' id='messageForm.recipients.1.email' name='messageForm.recipients.1.email' value='email2@email.com' /> 
</div>

数据类型为:

data ConfirmMessage = ConfirmMessage {
  isoDate :: T.Text,
  subject :: T.Text,
  body    :: T.Text,
  action  :: T.Text,
  recipients :: [Recipient]
} deriving (Show) 

data Recipient = Recipient {
  email :: T.Text 
} deriving (Show)

表格是:

messageForm :: (Monad m) => Form T.Text m ConfirmMessage
messageForm = ConfirmMessage
  <$> "isoDate"    .: stringRead "Missing ISOdate" Nothing
  <*> "subject"    .: stringRead "Subject required" Nothing
  <*> "body"       .: stringRead "Body required" Nothing
  <*> "action"     .: stringRead "Missing action" Nothing
  <*> "recipients" .: listOf recipientForm' Nothing

recipientForm' :: (Monad m) => Formlet T.Text m Recipient
recipientForm'= Recipient
  <$> "email"      .: text Nothing

我收到的错误消息是:

Couldn't match type `Text.Digestive.Form.Internal.FormTree
                       m0 v0 m0 Recipient'
              with `Maybe Recipient -> Form T.Text m Recipient'
Expected type: Formlet T.Text m Recipient
  Actual type: Text.Digestive.Form.Internal.FormTree
                 m0 v0 m0 Recipient
Relevant bindings include
  recipientForm' :: Formlet T.Text m Recipient
    (bound at src/Handler/TradeNotifyConfirm.hs:191:1)
In the expression: Recipient <$> "email" .: text Nothing
In an equation for `recipientForm'':
    recipientForm' = Recipient <$> "email" .: text Nothing

好的免责声明:我从未使用过digestive-functors,但我是这样处理这类问题的:

1) 您的错误消息显示 Expected type: Formlet T.Text m RecipientActual type: Text.Digestive.Form.Internal.FormTree m0 v0 m0 Recipient

2) 查看文档,看到:

type Formlet v m a = Maybe a -> Form v m a 
type Form v m a = FormTree m v m a

所以我们将上面的expected展开得到:

Formlet T.Text m Recipient 
-- v ~ T.Text, a ~ Recipient
Maybe Recipient -> Form T.Text m Recipient
--
Maybe Recipient -> FormTree m T.Text m Recipient

现在再看一遍:

  • 预期类型:Maybe Recipient -> FormTree m T.Text m Recipient
  • 实际类型:FormTree m0 v0 m0 Recipient

我们少了一个Maybe Recipient!从我上面看到的,我猜你只想忽略那个论点,因此我们有:

recipientForm' :: (Monad m) => Formlet T.Text m Recipient
recipientForm' _ = Recipient <$> "email" .: text Nothing

编译得很好,耶:)