我正在尝试为 Yesod 构建 bootstrap 3 兼容性。但是,通过创建“renderBootstrap3”函数来做到这一点是不可能的,因为您无法将类添加到输入中。所以,我选择在Form.Fields
. 这个想法是我可以克隆普通字段,但在属性数组中添加一个类声明。以下是相关代码:
import qualified Yesod.Form.Fields as F
injectClass :: (Text -> Text -> [(Text,Text)] -> Either Text a -> Bool -> WidgetT (HandlerSite m) IO ()
Text -> Text -> [(Text,Text)] -> Either Text a -> Bool -> WidgetT (HandlerSite m) IO ()
injectClass f a b attrs d e = f a b attrs d e
textField :: (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Text
textField = addInputClass F.textField
addInputClass :: (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m a -> Field m a
addInputClass f = f { fieldView = (injectClass $ fieldView f)}
因此,我的意图是采用普通版本的文本字段并使用记录语法仅修改 fieldView 方法。此方法应替换为除添加类属性外相同的方法。这还没有在上面的代码中实现。它可能看起来像:
injectClass f a b attrs d e = f a b (("class", "form-control") : attrs) d e
无论如何,问题是原始代码无法编译。我得到一个等式约束错误:
Could not deduce (HandlerSite m0 ~ HandlerSite m)
from the context (Monad m,
RenderMessage (HandlerSite m) FormMessage)
bound by the type signature for
addInputClass :: (Monad m,
RenderMessage (HandlerSite m) FormMessage) =>
Field m a -> Field m a
at Field/Bootstrap.hs:27:18-95
NB: `HandlerSite' is a type function, and may not be injective
The type variable `m0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Expected type: FieldViewFunc m a
Actual type: Text
-> Text
-> [(Text, Text)]
-> Either Text a
-> Bool
-> WidgetT (HandlerSite m0) IO ()
In the `fieldView' field of a record
In the expression: f {fieldView = (injectClass $ fieldView f)}
In an equation for `addInputClass':
addInputClass f = f {fieldView = (injectClass $ fieldView f)}
注意FieldViewFunc m a
定义为
type FieldViewFunc m a
= Text -- ^ ID
-> Text -- ^ Name
-> [(Text, Text)] -- ^ Attributes
-> Either Text a -- ^ Either (invalid text) or (legitimate result)
-> Bool -- ^ Required?
-> WidgetT (HandlerSite m) IO ()
所以,我离得不远了。问题是(我认为)它没有认识到injectClass
不会改变单子。但是,这对编译器来说应该是显而易见的。的类型签名injectClass
很清楚这一点。我正在寻找我需要做些什么来满足 GHC。感谢您的帮助,如果我能更清楚,请告诉我。