1

我正在关注改革库的教程。我目前有一个包含两个必填字段的表单及其设置,因此每个字段的错误消息都显示在它旁边。

postForm :: MyForm MyRequest
postForm = 
    MyRequest <$> name <*> msg <* inputSubmit "post it!"
  where
    name = labelText "name:"  ++>
            (inputText "" `transformEither` required "Name is required") <++ br <++ errorList
    msg  = labelText "message:" ++>
            (inputText "" `transformEither` required "Message is required") <++ br <++ errorList

例如,如果我将此表单提交为空,则验证将失败,它将要求用户再次填写它,它将如下所示:

name: [______]
  * Name is Required
message: [_____]
  * Message is required

我想知道如何为表单制作一个错误列表,如下所示:

name: [______]
message: [_____]
  * Name is Required
  * Message is required

我试着做

postForm :: MyForm MyRequest
postForm =
    (MyRequest <$> name <*> msg <* inputSubmit "post it!") <++ errorList
  where
    name = labelText "name:"  ++>
            (inputText "" `transformEither` required "Name is required") <++ br
    msg  = labelText "message:" ++>
            (inputText "" `transformEither` required "Message is required") <++ br

但随后错误消息根本不显示!


我没有包括一个完整的可运行示例,因为为此需要一堆额外的样板,我希望我的问题只是更好地理解应用符号。以下是一些有用的函数的类型签名:

type MyForm = Form (ServerPartT IO) [Input] ReqError H.Html ()

<++ :: (Monad m, Monoid view)
    => Form m input error view proof a
    -> Form m input error view () ()
    -> Form m input error view proof a

errorList :: (Monad m, ToMarkup error)
    => Form m input error Html () ()

http://hackage.haskell.org/package/reform-0.2.1/docs/Text-Reform-Core.html#v:-60--43--43-

http://hackage.haskell.org/package/reform-b​​laze-0.2.0/docs/Text-Reform-Blaze-Common.html#v:errorList

4

1 回答 1

1

不要使用errorList函数,而是使用childErrorList. 它还将包括来自子表单的错误。

(MyRequest <$> name <*> msg <* inputSubmit "post it!") <++ childErrorList

父表单周围的括号很重要。如果你这样做

MyRequest <$> name <*> msg <* inputSubmit "post it!" <++ childErrorList

然后运算符的优先级使其childErrorList仅从提交按钮(根本没有)而不是从整个表单中获取错误。

于 2013-12-27T02:52:40.623 回答