Web/Libraries/Formlets
From HaskellWiki
| Line 4: | Line 4: | ||
<haskell> | <haskell> | ||
| - | name :: | + | name :: Form String |
name = input Nothing | name = input Nothing | ||
</haskell> | </haskell> | ||
| Line 10: | Line 10: | ||
The input function takes a Maybe String, and produces a XHtmlForm String. The Maybe String is used for default values. If you give it a nothing, it won't have a default value. If you pass in a (Just "value"), it will be pre-populated with the value "value". | The input function takes a Maybe String, and produces a XHtmlForm String. The Maybe String is used for default values. If you give it a nothing, it won't have a default value. If you pass in a (Just "value"), it will be pre-populated with the value "value". | ||
| - | You can easily combine formlets using the | + | You can easily combine formlets using the Applicative Functor combinators. Suppose you have a User-datatype: |
<haskell> | <haskell> | ||
| Line 22: | Line 22: | ||
userForm = User <$> name <*> inputInteger <*> input Nothing | userForm = User <$> name <*> inputInteger <*> input Nothing | ||
</haskell> | </haskell> | ||
| + | |||
| + | You can also have more advanced widgets, like a radio-choice, that's where you use enumRadio: | ||
| + | |||
| + | <haskell> | ||
| + | enumRadio :: (Monad m, Enum a) => [(a, String)] -> Maybe a -> Form a | ||
| + | </haskell> | ||
| + | |||
| + | So it asks for a list of pairs with a value and the corresponding label, a possible default-value and it will return something of type a. | ||
| + | |||
| + | <haskell> | ||
| + | chooseBool :: Form Bool | ||
| + | chooseBool = enumRadio [(True, "Yes"), (False, "No")] True | ||
| + | </haskell> | ||
| + | |||
| + | Now we have a widget for booleans that we can use everywhere in our forms! | ||
== The basics == | == The basics == | ||
| + | |||
| + | === Simple validation === | ||
| + | === Monadic validation === | ||
== How it works == | == How it works == | ||
| Line 33: | Line 51: | ||
* [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/formlets Formlets library on hackage] | * [http://hackage.haskell.org/cgi-bin/hackage-scripts/package/formlets Formlets library on hackage] | ||
* [http://groups.inf.ed.ac.uk/links/formlets/ Papers on formlets] | * [http://groups.inf.ed.ac.uk/links/formlets/ Papers on formlets] | ||
| + | * [http://en.wikibooks.org/wiki/Haskell/Applicative_Functors Applicative Functors wikibook] | ||
Revision as of 09:13, 12 August 2008
Contents |
1 Introduction
Formlets are a way of building HTML forms that are type-safe, handle errors, abstract and are easy to combine into bigger forms. Here's an example:
name :: Form String name = input Nothing
The input function takes a Maybe String, and produces a XHtmlForm String. The Maybe String is used for default values. If you give it a nothing, it won't have a default value. If you pass in a (Just "value"), it will be pre-populated with the value "value".
You can easily combine formlets using the Applicative Functor combinators. Suppose you have a User-datatype:
data User = User {name :: String, age :: Integer, email :: String}
You can then build a form that produces a user:
userForm :: Form User userForm = User <$> name <*> inputInteger <*> input Nothing
You can also have more advanced widgets, like a radio-choice, that's where you use enumRadio:
enumRadio :: (Monad m, Enum a) => [(a, String)] -> Maybe a -> Form a
So it asks for a list of pairs with a value and the corresponding label, a possible default-value and it will return something of type a.
chooseBool :: Form Bool chooseBool = enumRadio [(True, "Yes"), (False, "No")] True
Now we have a widget for booleans that we can use everywhere in our forms!
