2

我已经能够在 snap 应用程序中成功地使用具有简单形式的消化函子;但是,我在尝试处理隐藏输入列表时遇到了困难。有些东西我不太了解如何使用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
4

1 回答 1

2

好的免责声明:我从未使用过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

所以我们从上面扩展期望得到:

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

编译得很好,耶:)

于 2015-08-13T18:20:29.330 回答