[英]How do I solve “HTTP request failed with: when is not a field” runtime errors in digestive-functors code?
我有一個簡單且不完整的Happstack應用程序,該應用程序由如下定義的摘要功能組成:
setRecFormSpec :: Monad m => Form Text m Reminder
setRecFormSpec = Reminder
<$> "progname" .: string Nothing
<*> "channel" .: string Nothing
<*> "when" .: localTimeFormlet "%d/%m/%Y" "%H:%M" Nothing
<*> "recordLimit" .: stringRead "Can't parse number" (Just 7)
其視圖定義如下:
setRecView :: View H.Html -> H.Html
setRecView view = do
H.div ! A.class_ "container" $ do
H.h1 "Set Record Reminder"
childErrorList "" view
divFormGroup $ do
label "progname" view "Program Name:"
formControl $ inputText "progname" view
divFormGroup $ do
label "channel" view "Channel:"
formControl $ inputText "channel" view
divFormGroup $ do
label "when" view "When:"
formControl $ inputDate "when" view
divFormGroup $ do
label "recordLimit" view "Recording Limit (days):"
formControl $ inputText "recordLimit" view
divFormGroup $ do
formControl $ inputSubmit "Signup"
-- divFormGroup -- candidate to go into a Bootstrap library
divFormGroup :: H.Html -> H.Html
divFormGroup h =
H.div ! A.class_ "form-group" $ h
-- formControl -- candidate to go into a Bootstrap library
formControl :: H.Html -> H.Html
formControl h = (h ! A.class_ "form-control")
提醒定義如下:
data Reminder = Reminder {
programName :: String -- ^ name of program
, channel :: String -- ^ name of broadcast channel
, firstShowing :: LocalTime -- ^ time of first showing
, timerPeriodDays :: Integer -- ^ how far in advance we can set timer, in days
} deriving (Show)
當我瀏覽到表單(/ setrec)的路徑時,我得到一個空白頁,控制台上印有以下錯誤消息:
HTTP request failed with: when is not a field
我發現了錯誤消息的定義位置(在https://github.com/jaspervdj/digestive-functors/blob/7e50d5686abc4b39389ed195693660d758987c7c/digestive-functors/src/Text/Digestive/Form/Internal.hs中 ),但我可以從那里看不到為什么找不到“時間”字段。
這里有什么問題?
如何調試此類問題?
這是指向github上整個代碼的鏈接,以防您需要更深入地研究代碼:
我在使用utcTimeFormlet
時遇到了相同的錯誤,該內部調用了localTimeFormlet
因此很可能是您遇到的相同錯誤。
看一下localTimeFormlet
的代碼
localTimeFormlet dFmt tFmt d = LocalTime
<$> "date" .: dateFormlet dFmt (localDay <$> d)
<*> "time" .: timeFormlet tFmt (localTimeOfDay <$> d)
如您所見,它由兩個subFormlet組成,這就是"when"
不是字段的原因。 它指的是帶有"date"
和"time"
作為實際字段的整體結構。
我使用的“解決方案”是完全跳過此功能,因為我實際上只需要一個日期,而不是精確的時間。 如果您的情況相同,那么您可能也想這樣做。
否則,您將不得不將這兩個字段合並到您的視圖中,並且它應該起作用。
我解決了這個問題,對偽半徑的解決方案做了一些改動。
我因此定義了一個新視圖:
dateTimeView view = do
divFormGroup $ do
label "date" view "When (Date):"
formControl $ inputText "date" view
divFormGroup $ do
label "time" view "When (Time):"
formControl $ inputText "time" view
在我的表單中,我將它與subView
一起subView
,因此:
setRecView :: View H.Html -> H.Html
setRecView view = do
H.div ! A.class_ "container" $ do
-- ...
divFormGroup $ do
label "channel" view "Channel:"
formControl $ inputText "channel" view
dateTimeView $ subView "when" view
divFormGroup $ do
label "recordLimit" view "Recording Limit (days):"
formControl $ inputText "recordLimit" view
divFormGroup $ do
formControl $ inputSubmit "Signup"
然后, runForm
調用便能夠找到when
字段並從中獲取日期和時間。
我可以使用dateLabelText和timeLabelText參數化dateTimeView,也許可以,但是以上內容足以說明問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.