Difference between revisions of "Web/Libraries/Formlets"

From HaskellWiki
< Web‎ | Libraries
Jump to navigation Jump to search
Line 9: Line 9:
   
 
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 combinators from Applicative. Suppose you have a User-datatype:
  +
  +
<haskell>
  +
data User = User {name :: String, age :: Integer, email :: String}
  +
</haskell>
  +
  +
You can then build a form that produces a user:
  +
  +
<haskell>
  +
userForm :: Form User
  +
userForm = User <$> name <*> inputInteger <*> input Nothing
  +
</haskell>
   
 
== The basics ==
 
== The basics ==
Line 18: Line 31:
 
== References ==
 
== References ==
   
[[http://hackage.haskell.org/cgi-bin/hackage-scripts/package/formlets]]
+
* [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]

Revision as of 09:07, 12 August 2008

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 :: XHtmlForm 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 combinators from Applicative. 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

The basics

How it works

Advanced: rolling your own output type

References