Questions tagged [formlets]

Formlets are a way of building HTML forms that are type-safe, handle errors, abstract and are easy to combine into bigger forms.

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!

2 questions
1
vote
1 answer

Why am I getting lifted/5.1 undefined error while using formlets?

I am having trouble with an error I keep getting while trying to use formlets in racket. It says: ; lifted/5.1: undefined; ; cannot reference an identifier before its definition ; in module: top-level ; internal name: lifted/5.1 Nothing in my…
Julian J
  • 13
  • 5
1
vote
1 answer

When creating a form with the Reform library, how do I put all the errors in a single list?

I'm following the tutorial for the reform library. I currently have a form with two required fields and its set up so the error message for each field shows up right next to it. postForm :: MyForm MyRequest postForm = MyRequest <$> name <*> msg…
hugomg
  • 68,213
  • 24
  • 160
  • 246