yesod-form-1.2.1.3: Form handling support for Yesod Web Framework

Safe Haskell | None

Yesod.Form.Functions

Contents

- Running in MForm monad
- Applicative/Monadic conversion
- Fields to Forms
- Run a form
- Generate a blank form
- Rendering
- Validation
- Utilities

Synopsis

- newFormIdent :: MForm sub master Text
- askParams :: MForm sub master (Maybe Env)
- askFiles :: MForm sub master (Maybe FileEnv)
- formToAForm :: MForm sub master (FormResult a, [FieldView sub master]) -> AForm sub master a
- aFormToForm :: AForm sub master a -> MForm sub master (FormResult a, [FieldView sub master] -> [FieldView sub master])
- mreq :: RenderMessage master FormMessage => Field sub master a -> FieldSettings master -> Maybe a -> MForm sub master (FormResult a, FieldView sub master)
- mopt :: Field sub master a -> FieldSettings master -> Maybe (Maybe a) -> MForm sub master (FormResult (Maybe a), FieldView sub master)
- areq :: RenderMessage master FormMessage => Field sub master a -> FieldSettings master -> Maybe a -> AForm sub master a
- aopt :: Field sub master a -> FieldSettings master -> Maybe (Maybe a) -> AForm sub master (Maybe a)
- runFormPost :: RenderMessage master FormMessage => (Markup -> MForm sub master (FormResult a, xml)) -> GHandler sub master ((FormResult a, xml), Enctype)
- runFormPostNoToken :: (Markup -> MForm sub master (FormResult a, xml)) -> GHandler sub master ((FormResult a, xml), Enctype)
- runFormGet :: (Markup -> MForm sub master a) -> GHandler sub master (a, Enctype)
- generateFormPost :: RenderMessage master FormMessage => (Markup -> MForm sub master (FormResult a, xml)) -> GHandler sub master (xml, Enctype)
- generateFormGet :: (Markup -> MForm sub master a) -> GHandler sub master (a, Enctype)
- type FormRender sub master a = AForm sub master a -> Markup -> MForm sub master (FormResult a, GWidget sub master ())
- renderTable :: FormRender sub master a
- renderDivs :: FormRender sub master a
- renderDivsNoLabels :: FormRender sub master a
- renderBootstrap :: FormRender sub master a
- check :: RenderMessage master msg => (a -> Either msg a) -> Field sub master a -> Field sub master a
- checkBool :: RenderMessage master msg => (a -> Bool) -> msg -> Field sub master a -> Field sub master a
- checkM :: RenderMessage master msg => (a -> GHandler sub master (Either msg a)) -> Field sub master a -> Field sub master a
- checkMMap :: RenderMessage master msg => (a -> GHandler sub master (Either msg b)) -> (b -> a) -> Field sub master a -> Field sub master b
- checkMMod :: RenderMessage master msg => (a -> GHandler sub master (Either msg b)) -> (b -> a) -> Field sub master a -> Field sub master b
- customErrorMessage :: SomeMessage master -> Field sub master a -> Field sub master a
- fieldSettingsLabel :: RenderMessage master msg => msg -> FieldSettings master
- aformM :: GHandler sub master a -> AForm sub master a
- parseHelper :: (Monad m, RenderMessage master FormMessage) => (Text -> Either FormMessage a) -> [Text] -> [FileInfo] -> m (Either (SomeMessage master) (Maybe a))

# Running in MForm monad

newFormIdent :: MForm sub master Text

Get a unique identifier.

askParams :: MForm sub master (Maybe Env)

askFiles :: MForm sub master (Maybe FileEnv)

# Applicative/Monadic conversion

formToAForm :: MForm sub master (FormResult a, [FieldView sub master]) -> AForm sub master a

aFormToForm :: AForm sub master a -> MForm sub master (FormResult a, [FieldView sub master] -> [FieldView sub master])

# Fields to Forms

mreq :: RenderMessage master FormMessage => Field sub master a -> FieldSettings master -> Maybe a -> MForm sub master (FormResult a, FieldView sub master)

mopt :: Field sub master a -> FieldSettings master -> Maybe (Maybe a) -> MForm sub master (FormResult (Maybe a), FieldView sub master)

areq :: RenderMessage master FormMessage => Field sub master a -> FieldSettings master -> Maybe a -> AForm sub master a

aopt :: Field sub master a -> FieldSettings master -> Maybe (Maybe a) -> AForm sub master (Maybe a)

# Run a form

runFormPost :: RenderMessage master FormMessage => (Markup -> MForm sub master (FormResult a, xml)) -> GHandler sub master ((FormResult a, xml), Enctype)

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`.

runFormPostNoToken :: (Markup -> MForm sub master (FormResult a, xml)) -> GHandler sub master ((FormResult a, xml), Enctype)

runFormGet :: (Markup -> MForm sub master a) -> GHandler sub master (a, Enctype)

# Generate a blank form

generateFormPost :: RenderMessage master FormMessage => (Markup -> MForm sub master (FormResult a, xml)) -> GHandler sub master (xml, Enctype)

Similar to `runFormPost`, except it always ignore the currently available environment. This is necessary in cases like a wizard UI, where a single page will both receive and incoming form and produce a new, blank form. For general usage, you can stick with `runFormPost`.

generateFormGet :: (Markup -> MForm sub master a) -> GHandler sub master (a, Enctype)

# Rendering

type FormRender sub master a = AForm sub master a -> Markup -> MForm sub master (FormResult a, GWidget sub master ())

renderTable :: FormRender sub master a

renderDivs :: FormRender sub master a

render a field inside a div

renderDivsNoLabels :: FormRender sub master a

render a field inside a div, not displaying any label

renderBootstrap :: FormRender sub master a

Render a form using Bootstrap-friendly shamlet syntax.

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}>

# Validation

check :: RenderMessage master msg => (a -> Either msg a) -> Field sub master a -> Field sub master a

checkBool :: RenderMessage master msg => (a -> Bool) -> msg -> Field sub master a -> Field sub master a

Return the given error message if the predicate is false.

checkM :: RenderMessage master msg => (a -> GHandler sub master (Either msg a)) -> Field sub master a -> Field sub master a

checkMMap :: RenderMessage master msg => (a -> GHandler sub master (Either msg b)) -> (b -> a) -> Field sub master a -> Field sub master 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

checkMMod :: RenderMessage master msg => (a -> GHandler sub master (Either msg b)) -> (b -> a) -> Field sub master a -> Field sub master b

Deprecated: Please use checkMMap instead

Deprecated synonym for `checkMMap`.

Since 1.1.1

customErrorMessage :: SomeMessage master -> Field sub master a -> Field sub master a

Allows you to overwrite the error message on parse error.

# Utilities

fieldSettingsLabel :: RenderMessage master msg => msg -> FieldSettings master

Generate a `FieldSettings` from the given label.

aformM :: GHandler sub master a -> AForm sub master a

Generate an `AForm` that gets its value from the given action.

parseHelper :: (Monad m, RenderMessage master FormMessage) => (Text -> Either FormMessage a) -> [Text] -> [FileInfo] -> m (Either (SomeMessage master) (Maybe a))

A helper function for creating custom fields.

This is intended to help with the common case where a single input value is required, such as when parsing a text field.

Since 1.1