消化函子和listOf

时间:2015-08-11 17:35:11

标签: haskell haskell-snap-framework digestive-functors

我已经能够在快照应用程序中成功使用digestive-functors和简单表单;但是,当我试图处理隐藏输入列表时,我会陷入困境。有一些我不太了解如何使用class Insurance::Policy < ActiveRecord::Base def build_models carrier = self.build_insurance_carrier carrier.build_insurance_carrier_settings end end 。谁能指出我正确的方向?感谢。

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

listOf

数据类型为:

<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

1 个答案:

答案 0 :(得分: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

编译得很好,yay:)