当我有一个持久性键的表单时,我通常更喜欢下拉菜单而不是手动输入。尝试下面的代码。另外,尝试遵循书中的惯用风格,它会有所帮助。
gs <- runInputPost $ GamingSession
start
Nothing
<$> ireq (selectField (optionsPersistKey [] [] (toPathPiece . entityKey))) "player"
<*> ireq (selectField (optionsPersistKey [] [] (toPathPiece . entityKey))) "table"
<*> iopt intField "seat"
-- | The optionsPersist builtin to the Yesod.Forms package unfortunately only
-- works well with whole persist entities. We are only interested in the entity
-- id s which is why we add in this function here:
optionsPersistKey
:: (YesodPersist master
, PersistEntity a
, PersistQuery (YesodPersistBackend master) (GHandler sub master)
, PathPiece (Key (YesodPersistBackend master) a)
, RenderMessage master msg
, PersistEntityBackend a ~ YesodPersistBackend master)
=> [Filter a]
-> [SelectOpt a]
-> (Entity a -> msg)
-> GHandler sub master (OptionList (Key (PersistEntityBackend a) a))
optionsPersistKey filts ords toDisplay = fmap mkOptionList $ do
mr <- getMessageRender
pairs <- runDB $ selectList filts ords
return $ map (\(Entity key value) -> Option
{ optionDisplay = mr (toDisplay $ Entity key value)
, optionInternalValue = key
, optionExternalValue = toPathPiece key
}) pairs
来源
2012-03-19 13:34:16
mxc
值看起来像 “4f6150251c21230c78000000”,这是一个id来自mongo – geoff 2012-03-19 16:47:09