HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleyesod-form-1.7.9Haskell2010

Yesod.Form.Option

  • 2 values
valueoptionsFromList'
  1. :: (MonadHandler m, RenderMessage (HandlerSite m) msg, PathPiece a)
  2. => [a]
  3. -> a -> msg
  4. -> m (OptionList a)
#

Creates an OptionList from a List, using the PathPiece instance for the external value and a custom function for the user-facing value.

PathPiece instances should provide suitable external values, since path pieces serve to be exposed through URLs or HTML anyway. Show/Read instances are avoided here since they could leak internal representations to forms, query params, javascript etc.

Example usage
data UserRole = URSalesTeam | URSalesHead | URTechTeam | URTechHead

instance PathPiece UserDepartment where
  toPathPiece = \case
    URSalesTeam -> "sales-team"
    URSalesHead -> "sales-head"
    URTechTeam -> "tech-team"
    URTechHead -> "tech-head"
  fromPathPiece = \case
    "sales-team" -> Just URSalesTeam
    "sales-head" -> Just URSalesHead
    "tech-team" -> Just URTechTeam
    "tech-head" -> Just URTechHead
    _ -> Nothing

userRoleOptions ::
  (MonadHandler m, RenderMessage (HandlerSite m) msg) => m (OptionList UserRole)
userRoleOptions = optionsFromList' userRoles toMsg
  where
  userRoles = [URSalesTeam, URSalesHead, URTechTeam, URTechHead]
  toMsg :: UserRole -> Text
  toMsg = \case
    URSalesTeam -> "Sales Team"
    URSalesHead -> "Head of Sales Team"
    URTechTeam -> "Tech Team"
    URTechHead -> "Head of Tech Team"

userRoleOptions, will produce an OptionList with the following attributes:

+----------------+----------------+--------------------+
| Internal Value | External Value | User-facing Value  |
+----------------+----------------+--------------------+
| URSalesTeam    | sales-team     | Sales Team         |
+----------------+----------------+--------------------+
| URSalesHead    | sales-head     | Head of Sales Team |
+----------------+----------------+--------------------+
| URTechTeam     | tech-team      | Tech Team          |
+----------------+----------------+--------------------+
| URTechHead     | tech-head      | Head of Tech Team  |
+----------------+----------------+--------------------+

Note that the type constraint allows localizable messages in place of toMsg (see https://en.wikipedia.org/wiki/Yesod_(web_framework)#Localizable_messages).