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

Safe Haskell | None
Language | Haskell2010

Yesod.Form

Description

Parse forms (and query strings).

Synopsis

- module Yesod.Form.Types
- module Yesod.Form.Functions
- data Option a = Option {
  - optionDisplay :: Text
  - optionInternalValue :: a
  - optionExternalValue :: Text
  }
- defaultFormMessage :: FormMessage -> Text
- textField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Text
- passwordField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Text
- textareaField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Textarea
- hiddenField :: forall (m :: Type -> Type) p. (Monad m, PathPiece p, RenderMessage (HandlerSite m) FormMessage) => Field m p
- intField :: forall (m :: Type -> Type) i. (Monad m, Integral i, RenderMessage (HandlerSite m) FormMessage) => Field m i
- dayField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Day
- timeField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m TimeOfDay
- timeFieldTypeTime :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m TimeOfDay
- timeFieldTypeText :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m TimeOfDay
- htmlField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Html
- emailField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Text
- multiEmailField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m [Text]
- searchField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => AutoFocus -> Field m Text
- type AutoFocus = Bool
- urlField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Text
- doubleField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Double
- parseDate :: String -> Either FormMessage Day
- parseTime :: Text -> Either FormMessage TimeOfDay
- newtype Textarea = Textarea {
  - unTextarea :: Text
  }
- boolField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Bool
- checkBoxField :: forall (m :: Type -> Type). Monad m => Field m Bool
- fileField :: forall (m :: Type -> Type). Monad m => Field m FileInfo
- fileAFormReq :: forall (m :: Type -> Type). (MonadHandler m, RenderMessage (HandlerSite m) FormMessage) => FieldSettings (HandlerSite m) -> AForm m FileInfo
- fileAFormOpt :: forall (m :: Type -> Type). MonadHandler m => FieldSettings (HandlerSite m) -> AForm m (Maybe FileInfo)
- selectFieldHelper :: (Eq a, RenderMessage site FormMessage) => (Text -> Text -> [(Text, Text)] -> WidgetFor site () -> WidgetFor site ()) -> (Text -> Text -> Bool -> WidgetFor site ()) -> (Text -> Text -> [(Text, Text)] -> Text -> Bool -> Text -> WidgetFor site ()) -> Maybe (Text -> WidgetFor site ()) -> HandlerFor site (OptionList a) -> Field (HandlerFor site) a
- selectField :: (Eq a, RenderMessage site FormMessage) => HandlerFor site (OptionList a) -> Field (HandlerFor site) a
- selectFieldList :: (Eq a, RenderMessage site FormMessage, RenderMessage site msg) => [(msg, a)] -> Field (HandlerFor site) a
- selectFieldListGrouped :: (Eq a, RenderMessage site FormMessage, RenderMessage site msg) => [(msg, [(msg, a)])] -> Field (HandlerFor site) a
- radioField :: (Eq a, RenderMessage site FormMessage) => HandlerFor site (OptionList a) -> Field (HandlerFor site) a
- radioField' :: (Eq a, RenderMessage site FormMessage) => HandlerFor site (OptionList a) -> Field (HandlerFor site) a
- radioFieldList :: (Eq a, RenderMessage site FormMessage, RenderMessage site msg) => [(msg, a)] -> Field (HandlerFor site) a
- withRadioField :: (Eq a, RenderMessage site FormMessage) => (Text -> WidgetFor site () -> WidgetFor site ()) -> (Text -> Text -> Bool -> Text -> WidgetFor site () -> WidgetFor site ()) -> HandlerFor site (OptionList a) -> Field (HandlerFor site) a
- checkboxesField :: Eq a => HandlerFor site (OptionList a) -> Field (HandlerFor site) [a]
- checkboxesField' :: Eq a => HandlerFor site (OptionList a) -> Field (HandlerFor site) [a]
- checkboxesFieldList :: (Eq a, RenderMessage site msg) => [(msg, a)] -> Field (HandlerFor site) [a]
- multiSelectField :: Eq a => HandlerFor site (OptionList a) -> Field (HandlerFor site) [a]
- multiSelectFieldList :: (Eq a, RenderMessage site msg) => [(msg, a)] -> Field (HandlerFor site) [a]
- data OptionList a
  - = OptionList {
    - olOptions :: [Option a]
    - olReadExternal :: Text -> Maybe a
    }
  - | OptionListGrouped {
    - olOptionsGrouped :: [(Text, [Option a])]
    - olReadExternalGrouped :: Text -> Maybe a
    }
- mkOptionList :: [Option a] -> OptionList a
- mkOptionListGrouped :: [(Text, [Option a])] -> OptionList a
- optionsPersist :: (YesodPersist site, PersistQueryRead backend, PathPiece (Key a), RenderMessage site msg, YesodPersistBackend site ~ backend, PersistRecordBackend a backend) => [Filter a] -> [SelectOpt a] -> (a -> msg) -> HandlerFor site (OptionList (Entity a))
- optionsPersistKey :: (YesodPersist site, PersistQueryRead backend, PathPiece (Key a), RenderMessage site msg, backend ~ YesodPersistBackend site, PersistRecordBackend a backend) => [Filter a] -> [SelectOpt a] -> (a -> msg) -> HandlerFor site (OptionList (Key a))
- optionsPairs :: (MonadHandler m, RenderMessage (HandlerSite m) msg) => [(msg, a)] -> m (OptionList a)
- optionsPairsGrouped :: (MonadHandler m, RenderMessage (HandlerSite m) msg) => [(msg, [(msg, a)])] -> m (OptionList a)
- optionsEnum :: (MonadHandler m, Show a, Enum a, Bounded a) => m (OptionList a)
- colorField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Text
- datetimeLocalField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m LocalTime
- module Yesod.Form.Input

# Documentation

module Yesod.Form.Types

module Yesod.Form.Functions

data Option a Source #

Constructors

Option |

Fields

- optionDisplay :: Text

  The user-facing label.

- optionInternalValue :: a

  The Haskell value being selected.

- optionExternalValue :: Text

  The representation of this value stored in the form.

#### Instances

Instances details

Functor Option Source # | Since: 1.4.6
Instance details Defined in Yesod.Form.Fields Methods fmap :: (a -> b) -> Option a -> Option b Source # (<$) :: a -> Option b -> Option a Source #

defaultFormMessage :: FormMessage -> Text Source #

textField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Text Source #

Creates a input with `type="text"`.

passwordField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Text Source #

Creates an input with `type="password"`.

textareaField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Textarea Source #

Creates a `<textarea>` tag whose returned value is wrapped in a `Textarea`; see `Textarea` for details.

hiddenField :: forall (m :: Type -> Type) p. (Monad m, PathPiece p, RenderMessage (HandlerSite m) FormMessage) => Field m p Source #

Creates an input with `type="hidden"`; you can use this to store information in a form that users shouldn't see (for example, Yesod stores CSRF tokens in a hidden field).

intField :: forall (m :: Type -> Type) i. (Monad m, Integral i, RenderMessage (HandlerSite m) FormMessage) => Field m i Source #

Creates a input with `type="number"` and `step=1`.

dayField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Day Source #

Creates an input with `type="date"`, validating the input using the `parseDate` function.

Add the `time` package and import the Data.Time.Calendar module to use this function.

timeField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m TimeOfDay Source #

An alias for `timeFieldTypeTime`.

timeFieldTypeTime :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m TimeOfDay Source #

Creates an input with `type="time"`. Browsers not supporting this type will fallback to a text field, and Yesod will parse the time as described in `timeFieldTypeText`.

Add the `time` package and import the Data.Time.LocalTime module to use this function.

Since: 1.4.2

timeFieldTypeText :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m TimeOfDay Source #

Creates an input with `type="text"`, parsing the time from an [H]H:MM[:SS] format, with an optional AM or PM (if not given, AM is assumed for compatibility with the 24 hour clock system).

This function exists for backwards compatibility with the old implementation of `timeField`, which used to use `type="text"`. Consider using `timeField` or `timeFieldTypeTime` for improved UX and validation from the browser.

Add the `time` package and import the Data.Time.LocalTime module to use this function.

Since: 1.4.2

htmlField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Html Source #

Creates a `<textarea>` tag whose input is sanitized to prevent XSS attacks and is validated for having balanced tags.

emailField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Text Source #

Creates an input with `type="email"`. Yesod will validate the email's correctness according to RFC5322 and canonicalize it by removing comments and whitespace (see Text.Email.Validate).

multiEmailField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m [Text] Source #

Creates an input with `type="email"` with the multiple attribute; browsers might implement this as taking a comma separated list of emails. Each email address is validated as described in `emailField`.

Since: 1.3.7

searchField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => AutoFocus -> Field m Text Source #

Creates an input with `type="search"`. For browsers without autofocus support, a JS fallback is used if `AutoFocus` is true.

type AutoFocus = Bool Source #

urlField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Text Source #

Creates an input with `type="url"`, validating the URL according to RFC3986.

doubleField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Double Source #

Creates a input with `type="number"` and `step=any`.

parseDate :: String -> Either FormMessage Day Source #

Parses a `Day` from a `String`.

parseTime :: Text -> Either FormMessage TimeOfDay Source #

newtype Textarea Source #

A newtype wrapper around a `Text` whose `ToMarkup` instance converts newlines to HTML `<br>` tags.

(When text is entered into a `<textarea>`, newline characters are used to separate lines. If this text is then placed verbatim into HTML, the lines won't be separated, thus the need for replacing with `<br>` tags). If you don't need this functionality, simply use `unTextarea` to access the raw text.

Constructors

Textarea |

Fields

- unTextarea :: Text

#### Instances

Instances details

FromJSON Textarea Source # |
Instance details Defined in Yesod.Form.Fields Methods parseJSON :: Value -> Parser Textarea Source # parseJSONList :: Value -> Parser [Textarea] Source # omittedField :: Maybe Textarea Source #
ToJSON Textarea Source # |
Instance details Defined in Yesod.Form.Fields Methods toJSON :: Textarea -> Value Source # toEncoding :: Textarea -> Encoding Source # toJSONList :: [Textarea] -> Value Source # toEncodingList :: [Textarea] -> Encoding Source # omitField :: Textarea -> Bool Source #
ToMarkup Textarea Source # |
Instance details Defined in Yesod.Form.Fields Methods toMarkup :: Textarea -> Markup Source # preEscapedToMarkup :: Textarea -> Markup Source #
IsString Textarea Source # |
Instance details Defined in Yesod.Form.Fields Methods fromString :: String -> Textarea Source #
Read Textarea Source # |
Instance details Defined in Yesod.Form.Fields Methods readsPrec :: Int -> ReadS Textarea Source # readList :: ReadS [Textarea] Source # readPrec :: ReadPrec Textarea Source # readListPrec :: ReadPrec [Textarea] Source #
Show Textarea Source # |
Instance details Defined in Yesod.Form.Fields Methods showsPrec :: Int -> Textarea -> ShowS Source # show :: Textarea -> String Source # showList :: [Textarea] -> ShowS Source #
Eq Textarea Source # |
Instance details Defined in Yesod.Form.Fields Methods (==) :: Textarea -> Textarea -> Bool Source # (/=) :: Textarea -> Textarea -> Bool Source #
Ord Textarea Source # |
Instance details Defined in Yesod.Form.Fields Methods compare :: Textarea -> Textarea -> Ordering Source # (<) :: Textarea -> Textarea -> Bool Source # (<=) :: Textarea -> Textarea -> Bool Source # (>) :: Textarea -> Textarea -> Bool Source # (>=) :: Textarea -> Textarea -> Bool Source # max :: Textarea -> Textarea -> Textarea Source # min :: Textarea -> Textarea -> Textarea Source #
PersistField Textarea Source # |
Instance details Defined in Yesod.Form.Fields Methods toPersistValue :: Textarea -> PersistValue Source # fromPersistValue :: PersistValue -> Either Text Textarea Source #
PersistFieldSql Textarea Source # |
Instance details Defined in Yesod.Form.Fields Methods sqlType :: Proxy Textarea -> SqlType Source #

boolField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Bool Source #

Creates a group of radio buttons to answer the question given in the message. Radio buttons are used to allow differentiating between an empty response (`Nothing`) and a no response (`Just False`). Consider using the simpler `checkBoxField` if you don't need to make this distinction.

If this field is optional, the first radio button is labeled "<None>", the second "Yes" and the third "No".

If this field is required, the first radio button is labeled "Yes" and the second "No".

(Exact label titles will depend on localization).

checkBoxField :: forall (m :: Type -> Type). Monad m => Field m Bool Source #

Creates an input with `type="checkbox"`. While the default ``boolField`` implements a radio button so you can differentiate between an empty response (`Nothing`) and a no response (`Just False`), this simpler checkbox field returns an empty response as `Just False`.

Note that this makes the field always optional.

fileField :: forall (m :: Type -> Type). Monad m => Field m FileInfo Source #

Creates an input with `type="file"`.

fileAFormReq :: forall (m :: Type -> Type). (MonadHandler m, RenderMessage (HandlerSite m) FormMessage) => FieldSettings (HandlerSite m) -> AForm m FileInfo Source #

fileAFormOpt :: forall (m :: Type -> Type). MonadHandler m => FieldSettings (HandlerSite m) -> AForm m (Maybe FileInfo) Source #

selectFieldHelper Source #

Arguments

:: (Eq a, RenderMessage site FormMessage) |
=> (Text -> Text -> [(Text, Text)] -> WidgetFor site () -> WidgetFor site ()) | Outermost part of the field
-> (Text -> Text -> Bool -> WidgetFor site ()) | An option for None if the field is optional
-> (Text -> Text -> [(Text, Text)] -> Text -> Bool -> Text -> WidgetFor site ()) | Other options
-> Maybe (Text -> WidgetFor site ()) | Group headers placed inbetween options
-> HandlerFor site (OptionList a) |
-> Field (HandlerFor site) a |

A helper function for constucting `selectField`s with optional option groups. You may want to use this when you define your custom `selectField`s or `radioField`s.

Since: 1.6.2

selectField :: (Eq a, RenderMessage site FormMessage) => HandlerFor site (OptionList a) -> Field (HandlerFor site) a Source #

Creates a `<select>` tag with optional `<optgroup>`s for selecting one option. Example usage:

    areq (selectField $ optionsPairs [(MsgValue1, "value1"),(MsgValue2, "value2")]) "Which value?" Nothing

selectFieldList :: (Eq a, RenderMessage site FormMessage, RenderMessage site msg) => [(msg, a)] -> Field (HandlerFor site) a Source #

Creates a `<select>` tag for selecting one option. Example usage:

    areq (selectFieldList [("Value 1" :: Text, "value1"),("Value 2", "value2")]) "Which value?" Nothing

selectFieldListGrouped :: (Eq a, RenderMessage site FormMessage, RenderMessage site msg) => [(msg, [(msg, a)])] -> Field (HandlerFor site) a Source #

Creates a `<select>` tag with `<optgroup>`s for selecting one option.

Since: 1.7.0

radioField :: (Eq a, RenderMessage site FormMessage) => HandlerFor site (OptionList a) -> Field (HandlerFor site) a Source #

Deprecated: This function seems to have a bug (label could not be found with byLabel algorithm)

Creates an input with `type="radio"` for selecting one option.

radioField' :: (Eq a, RenderMessage site FormMessage) => HandlerFor site (OptionList a) -> Field (HandlerFor site) a Source #

Creates an input with `type="radio"` for selecting one option.

Since: 1.7.8

radioFieldList :: (Eq a, RenderMessage site FormMessage, RenderMessage site msg) => [(msg, a)] -> Field (HandlerFor site) a Source #

Creates an input with `type="radio"` for selecting one option.

withRadioField Source #

Arguments

:: (Eq a, RenderMessage site FormMessage) |
=> (Text -> WidgetFor site () -> WidgetFor site ()) | nothing case for mopt
-> (Text -> Text -> Bool -> Text -> WidgetFor site () -> WidgetFor site ()) | cases for values
-> HandlerFor site (OptionList a) |
-> Field (HandlerFor site) a |

Allows the user to place the option radio widget somewhere in the template. For example: If you want a table of radio options to select. `radioField` is an example on how to use this function.

Since: 1.7.2

checkboxesField :: Eq a => HandlerFor site (OptionList a) -> Field (HandlerFor site) [a] Source #

Deprecated: This function seems to have a bug (label could not be found with byLabel algorithm)

Creates an input with `type="checkbox"` for selecting multiple options.

checkboxesField' :: Eq a => HandlerFor site (OptionList a) -> Field (HandlerFor site) [a] Source #

Creates an input with `type="checkbox"` for selecting multiple options.

checkboxesFieldList :: (Eq a, RenderMessage site msg) => [(msg, a)] -> Field (HandlerFor site) [a] Source #

Creates an input with `type="checkbox"` for selecting multiple options.

multiSelectField :: Eq a => HandlerFor site (OptionList a) -> Field (HandlerFor site) [a] Source #

Creates a `<select>` tag for selecting multiple options.

multiSelectFieldList :: (Eq a, RenderMessage site msg) => [(msg, a)] -> Field (HandlerFor site) [a] Source #

Creates a `<select>` tag for selecting multiple options.

data OptionList a Source #

A structure holding a list of options. Typically you can use a convenience function like `mkOptionList` or `optionsPairs` instead of creating this directly.

Extended by `OptionListGrouped` in 1.7.0.

Constructors

OptionList |

Fields

- olOptions :: [Option a]
- olReadExternal :: Text -> Maybe a

  A function mapping from the form's value (`optionExternalValue`) to the selected Haskell value (`optionInternalValue`).

OptionListGrouped |

Fields

- olOptionsGrouped :: [(Text, [Option a])]
- olReadExternalGrouped :: Text -> Maybe a

  A function mapping from the form's value (`optionExternalValue`) to the selected Haskell value (`optionInternalValue`).

#### Instances

Instances details

Functor OptionList Source # | Since: 1.4.6
Instance details Defined in Yesod.Form.Fields Methods fmap :: (a -> b) -> OptionList a -> OptionList b Source # (<$) :: a -> OptionList b -> OptionList a Source #

mkOptionList :: [Option a] -> OptionList a Source #

Creates an `OptionList`, using a `Map` to implement the `olReadExternal` function.

mkOptionListGrouped :: [(Text, [Option a])] -> OptionList a Source #

Creates an `OptionList`, using a `Map` to implement the `olReadExternalGrouped` function.

Since: 1.7.0

optionsPersist :: (YesodPersist site, PersistQueryRead backend, PathPiece (Key a), RenderMessage site msg, YesodPersistBackend site ~ backend, PersistRecordBackend a backend) => [Filter a] -> [SelectOpt a] -> (a -> msg) -> HandlerFor site (OptionList (Entity a)) Source #

Selects a list of `Entity`s with the given `Filter` and `SelectOpt`s. The `(a -> msg)` function is then used to derive the display value for an `OptionList`. Example usage:

    Country
       name Text
       deriving Eq -- Must derive Eq

    data CountryForm = CountryForm
      { country :: Entity Country
      }

    countryNameForm :: AForm Handler CountryForm
    countryNameForm = CountryForm
            <$> areq (selectField countries) "Which country do you live in?" Nothing
            where
              countries = optionsPersist [] [Asc CountryName] countryName

optionsPersistKey :: (YesodPersist site, PersistQueryRead backend, PathPiece (Key a), RenderMessage site msg, backend ~ YesodPersistBackend site, PersistRecordBackend a backend) => [Filter a] -> [SelectOpt a] -> (a -> msg) -> HandlerFor site (OptionList (Key a)) Source #

An alternative to `optionsPersist` which returns just the `Key` instead of the entire `Entity`.

Since: 1.3.2

optionsPairs :: (MonadHandler m, RenderMessage (HandlerSite m) msg) => [(msg, a)] -> m (OptionList a) Source #

Creates an `OptionList` from a list of (display-value, internal value) pairs.

optionsPairsGrouped :: (MonadHandler m, RenderMessage (HandlerSite m) msg) => [(msg, [(msg, a)])] -> m (OptionList a) Source #

Creates an `OptionList` from a list of (display-value, internal value) pairs.

Since: 1.7.0

optionsEnum :: (MonadHandler m, Show a, Enum a, Bounded a) => m (OptionList a) Source #

Creates an `OptionList` from an `Enum`, using its `Show` instance for the user-facing value.

colorField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m Text Source #

Creates an input with `type="color"`. The input value must be provided in hexadecimal format #rrggbb.

Since: 1.7.1

datetimeLocalField :: forall (m :: Type -> Type). (Monad m, RenderMessage (HandlerSite m) FormMessage) => Field m LocalTime Source #

Creates an input with `type="datetime-local"`. The input value must be provided in YYYY-MM-DD(T| )HH:MM[:SS] format.

Since: 1.7.6

module Yesod.Form.Input