yesod-form-1.3.0: 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 :: Monad m => MForm m Text
- askParams :: Monad m => MForm m (Maybe Env)
- askFiles :: Monad m => MForm m (Maybe FileEnv)
- formToAForm :: (HandlerSite m ~ site, Monad m) => MForm m (FormResult a, [FieldView site]) -> AForm m a
- aFormToForm :: (Monad m, HandlerSite m ~ site) => AForm m a -> MForm m (FormResult a, [FieldView site] -> [FieldView site])
- mreq :: (RenderMessage site FormMessage, HandlerSite m ~ site, MonadHandler m) => Field m a -> FieldSettings site -> Maybe a -> MForm m (FormResult a, FieldView site)
- mopt :: (site ~ HandlerSite m, MonadHandler m) => Field m a -> FieldSettings site -> Maybe (Maybe a) -> MForm m (FormResult (Maybe a), FieldView site)
- areq :: (RenderMessage site FormMessage, HandlerSite m ~ site, MonadHandler m) => Field m a -> FieldSettings site -> Maybe a -> AForm m a
- aopt :: MonadHandler m => Field m a -> FieldSettings (HandlerSite m) -> Maybe (Maybe a) -> AForm m (Maybe a)
- runFormPost :: (RenderMessage (HandlerSite m) FormMessage, MonadResource m, MonadHandler m) => (Markup -> MForm m (FormResult a, xml)) -> m ((FormResult a, xml), Enctype)
- runFormPostNoToken :: MonadHandler m => (Markup -> MForm m (FormResult a, xml)) -> m ((FormResult a, xml), Enctype)
- runFormGet :: MonadHandler m => (Markup -> MForm m a) -> m (a, Enctype)
- generateFormPost :: (RenderMessage (HandlerSite m) FormMessage, MonadHandler m) => (Markup -> MForm m (FormResult a, xml)) -> m (xml, Enctype)
- generateFormGet :: MonadHandler m => (Markup -> MForm m a) -> m (a, Enctype)
- type FormRender m a = AForm m a -> Markup -> MForm m (FormResult a, WidgetT (HandlerSite m) IO ())
- renderTable :: Monad m => FormRender m a
- renderDivs :: Monad m => FormRender m a
- renderDivsNoLabels :: Monad m => FormRender m a
- renderBootstrap :: Monad m => FormRender m a
- check :: (Monad m, RenderMessage (HandlerSite m) msg) => (a -> Either msg a) -> Field m a -> Field m a
- checkBool :: (Monad m, RenderMessage (HandlerSite m) msg) => (a -> Bool) -> msg -> Field m a -> Field m a
- checkM :: (Monad m, RenderMessage (HandlerSite m) msg) => (a -> m (Either msg a)) -> Field m a -> Field m a
- checkMMap :: (Monad m, RenderMessage (HandlerSite m) msg) => (a -> m (Either msg b)) -> (b -> a) -> Field m a -> Field m b
- customErrorMessage :: Monad m => SomeMessage (HandlerSite m) -> Field m a -> Field m a
- fieldSettingsLabel :: RenderMessage site msg => msg -> FieldSettings site
- parseHelper :: (Monad m, RenderMessage site FormMessage) => (Text -> Either FormMessage a) -> [Text] -> [FileInfo] -> m (Either (SomeMessage site) (Maybe a))

# Running in MForm monad

newFormIdent :: Monad m => MForm m Text

Get a unique identifier.

askParams :: Monad m => MForm m (Maybe Env)

askFiles :: Monad m => MForm m (Maybe FileEnv)

# Applicative/Monadic conversion

formToAForm :: (HandlerSite m ~ site, Monad m) => MForm m (FormResult a, [FieldView site]) -> AForm m a

aFormToForm :: (Monad m, HandlerSite m ~ site) => AForm m a -> MForm m (FormResult a, [FieldView site] -> [FieldView site])

# Fields to Forms

mreq :: (RenderMessage site FormMessage, HandlerSite m ~ site, MonadHandler m) => Field m a -> FieldSettings site -> Maybe a -> MForm m (FormResult a, FieldView site)

mopt :: (site ~ HandlerSite m, MonadHandler m) => Field m a -> FieldSettings site -> Maybe (Maybe a) -> MForm m (FormResult (Maybe a), FieldView site)

areq :: (RenderMessage site FormMessage, HandlerSite m ~ site, MonadHandler m) => Field m a -> FieldSettings site -> Maybe a -> AForm m a

aopt :: MonadHandler m => Field m a -> FieldSettings (HandlerSite m) -> Maybe (Maybe a) -> AForm m (Maybe a)

# Run a form

runFormPost :: (RenderMessage (HandlerSite m) FormMessage, MonadResource m, MonadHandler m) => (Markup -> MForm m (FormResult a, xml)) -> m ((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 :: MonadHandler m => (Markup -> MForm m (FormResult a, xml)) -> m ((FormResult a, xml), Enctype)

runFormGet :: MonadHandler m => (Markup -> MForm m a) -> m (a, Enctype)

# Generate a blank form

generateFormPost :: (RenderMessage (HandlerSite m) FormMessage, MonadHandler m) => (Markup -> MForm m (FormResult a, xml)) -> m (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 :: MonadHandler m => (Markup -> MForm m a) -> m (a, Enctype)

# Rendering

type FormRender m a = AForm m a -> Markup -> MForm m (FormResult a, WidgetT (HandlerSite m) IO ())

renderTable :: Monad m => FormRender m a

renderDivs :: Monad m => FormRender m a

render a field inside a div

renderDivsNoLabels :: Monad m => FormRender m a

render a field inside a div, not displaying any label

renderBootstrap :: Monad m => FormRender m 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 :: (Monad m, RenderMessage (HandlerSite m) msg) => (a -> Either msg a) -> Field m a -> Field m a

checkBool :: (Monad m, RenderMessage (HandlerSite m) msg) => (a -> Bool) -> msg -> Field m a -> Field m a

Return the given error message if the predicate is false.

checkM :: (Monad m, RenderMessage (HandlerSite m) msg) => (a -> m (Either msg a)) -> Field m a -> Field m a

checkMMap :: (Monad m, RenderMessage (HandlerSite m) msg) => (a -> m (Either msg b)) -> (b -> a) -> Field m a -> 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

customErrorMessage :: Monad m => SomeMessage (HandlerSite m) -> Field m a -> Field m a

Allows you to overwrite the error message on parse error.

# Utilities

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

Generate a `FieldSettings` from the given label.

parseHelper :: (Monad m, RenderMessage site FormMessage) => (Text -> Either FormMessage a) -> [Text] -> [FileInfo] -> m (Either (SomeMessage site) (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