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

Running in MForm monad

3 declarations

Applicative/Monadic conversion

5 declarations

Fields to Forms

9 declarations
valuewreqMsg
  1. :: (RenderMessage site msg, HandlerSite m ~ site, MonadHandler m)
  2. => Field m a

    form field

  3. -> FieldSettings site

    settings for this field

  4. -> msg

    message to use in case value is Nothing

  5. -> Maybe a

    optional default value

  6. -> WForm m (FormResult a)
#

Same as wreq but with your own message to be rendered in case the value is not provided.

This is useful when you have several required fields on the page and you want to differentiate between which fields were left blank. Otherwise the user sees "Value is required" multiple times, which is ambiguous.

valuemreqMsg
  1. :: (RenderMessage site msg, HandlerSite m ~ site, MonadHandler m)
  2. => Field m a

    form field

  3. -> FieldSettings site

    settings for this field

  4. -> msg

    Message to use in case value is Nothing

  5. -> Maybe a

    optional default value

  6. -> MForm m (FormResult a, FieldView site)
#

Same as mreq but with your own message to be rendered in case the value is not provided.

This is useful when you have several required fields on the page and you want to differentiate between which fields were left blank. Otherwise the user sees "Value is required" multiple times, which is ambiguous.

valueareqMsg
  1. :: (RenderMessage site msg, HandlerSite m ~ site, MonadHandler m)
  2. => Field m a

    form field

  3. -> FieldSettings site

    settings for this field

  4. -> msg

    message to use in case value is Nothing

  5. -> Maybe a

    optional default value

  6. -> AForm m a
#

Same as areq but with your own message to be rendered in case the value is not provided.

This is useful when you have several required fields on the page and you want to differentiate between which fields were left blank. Otherwise the user sees "Value is required" multiple times, which is ambiguous.

Run a form

3 declarations

This function is used to both initially render a form and to later extract results from it. Note that, due to CSRF protection and a few other issues, forms submitted via GET and POST are slightly different. As such, be sure to call the relevant function based on how the form will be submitted, not the current request method.

For example, a common case is displaying a form on a GET request and having the form submit to a POST page. In such a case, both the GET and POST handlers should use runFormPost.

Generate a blank form

3 declarations

More than one form on a handler

1 declaration
valueidentifyForm
  1. :: Monad m
  2. => Text

    Form identification string.

  3. -> (Markup -> MForm m (FormResult a, WidgetFor (HandlerSite m) ()))
  4. -> Markup
  5. -> MForm m (FormResult a, WidgetFor (HandlerSite m) ())
#

Creates a hidden field on the form that identifies it. This identification is then used to distinguish between missing and wrong form data when a single handler contains more than one form.

For instance, if you have the following code on your handler:

((fooRes, fooWidget), fooEnctype) <- runFormPost fooForm
((barRes, barWidget), barEnctype) <- runFormPost barForm

Then replace it with

((fooRes, fooWidget), fooEnctype) <- runFormPost $ identifyForm "foo" fooForm
((barRes, barWidget), barEnctype) <- runFormPost $ identifyForm "bar" barForm

Note that it's your responsibility to ensure that the identification strings are unique (using the same one twice on a single handler will not generate any errors). This allows you to create a variable number of forms and still have them work even if their number or order change between the HTML generation and the form submission.

Rendering

6 declarations
valuerenderTable :: Monad m => FormRender m a
#

Render a form into a series of tr tags. Note that, in order to allow you to add extra rows to the table, this function does not wrap up the resulting HTML in a table tag; you must do that yourself.

valuerenderBootstrap2 :: Monad m => FormRender m a
#

Render a form using Bootstrap v2-friendly shamlet syntax. If you're using Bootstrap v3, then you should use the functions from module Yesod.Form.Bootstrap3.

Sample Hamlet:

 <form .form-horizontal method=post action=@{ActionR} enctype=#{formEnctype}>
   <fieldset>
     <legend>_{MsgLegend}
     $case result
       $of FormFailure reasons
         $forall reason <- reasons
           <div .alert .alert-error>#{reason}
       $of _
     ^{formWidget}
     <div .form-actions>
       <input .btn .primary type=submit value=_{MsgSubmit}>

Since 1.3.14

Validation

5 declarations
valuecheckMMap
  1. :: (Monad m, RenderMessage (HandlerSite m) msg)
  2. => a -> m (Either msg b)
  3. -> b -> a
  4. -> Field m a
  5. -> Field m b
#

Same as checkM, but modifies the datatype.

In order to make this work, you must provide a function to convert back from the new datatype to the old one (the second argument to this function).

Since 1.1.2

Utilities

6 declarations
valueconvertField :: Functor m => (a -> b) -> (b -> a) -> Field m a -> Field m b
#

Since a Field cannot be a Functor, it is not obvious how to "reuse" a Field on a newtype or otherwise equivalent type. This function allows you to convert a Field m a to a Field m b assuming you provide a bidirectional conversion between the two, through the first two functions.

A simple example:

import Data.Monoid
sumField :: (Functor m, Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m (Sum Int)
sumField = convertField Sum getSum intField

Another example, not using a newtype, but instead creating a Lazy Text field:

import qualified Data.Text.Lazy as TL
TextField :: (Functor m, Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m TL.Text
lazyTextField = convertField TL.fromStrict TL.toStrict textField

Since 1.3.16

valueaddClass
  1. :: Text

    The class to add

  2. -> [(Text, Text)]

    List of existing fsAttrs

  3. -> [(Text, Text)]
#

Adds a CSS class to the fsAttrs in a FieldSettings.

Examples
Example1 expression
addClass "login-form" [("class", "form-control"), ("id", "home-login")][("class","form-control login-form"),("id","home-login")]
valueremoveClass
  1. :: Text

    The class to remove

  2. -> [(Text, Text)]

    List of existing fsAttrs

  3. -> [(Text, Text)]
#

Removes a CSS class from the fsAttrs in a FieldSettings.

Examples
Example1 expression
removeClass "form-control" [("class","form-control login-form"),("id","home-login")][("class","  login-form"),("id","home-login")]