From b8d47f880f1211fd59f1a7a267992f2d2a64bcf8 Mon Sep 17 00:00:00 2001 From: andrewthad Date: Wed, 28 Aug 2013 15:14:08 -0400 Subject: [PATCH] Added optionsPersistKey to Fields There is no immediately obvious way to use optionsPersist with selectField. The function optionsPersistKey makes this possible. Example of use: areq (selectField $ optionsPersistKey [] [Asc UserIdent] userIdent) "User" Nothing --- yesod-form/Yesod/Form/Fields.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/yesod-form/Yesod/Form/Fields.hs b/yesod-form/Yesod/Form/Fields.hs index bd675b56..a1e4a1c3 100644 --- a/yesod-form/Yesod/Form/Fields.hs +++ b/yesod-form/Yesod/Form/Fields.hs @@ -42,6 +42,7 @@ module Yesod.Form.Fields , OptionList (..) , mkOptionList , optionsPersist + , optionsPersistKey , optionsPairs , optionsEnum ) where @@ -513,6 +514,27 @@ optionsPersist filts ords toDisplay = fmap mkOptionList $ do , optionExternalValue = toPathPiece key }) pairs +optionsPersistKey + :: (YesodPersist site + , PersistEntity a + , PersistQuery (YesodPersistBackend site (HandlerT site IO)) + , PathPiece (Key a) + , RenderMessage site msg + , PersistEntityBackend a ~ PersistMonadBackend (YesodDB site)) + => [Filter a] + -> [SelectOpt a] + -> (a -> msg) + -> HandlerT site IO (OptionList (Key a)) + +optionsPersistKey filts ords toDisplay = fmap mkOptionList $ do + mr <- getMessageRender + pairs <- runDB $ selectList filts ords + return $ Import.map (\(Entity key value) -> Option + { optionDisplay = mr (toDisplay value) + , optionInternalValue = key + , optionExternalValue = toPathPiece key + }) pairs + selectFieldHelper :: (Eq a, RenderMessage site FormMessage) => (Text -> Text -> [(Text, Text)] -> WidgetT site IO () -> WidgetT site IO ())