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

Yesod.Form

Contents

Description

Parse forms (and query strings).

Synopsis

Data types

data GForm s m xml a Source

A generic form, allowing you to specifying the subsite datatype, master site datatype, a datatype for the form XML and the return type.

Instances

Monoid xml => Functor (GForm sub url xml) 
Monoid xml => Applicative (GForm sub url xml) 
IsForm (FormField s m a) 
RunForm (GForm s m xml a) 

data FormResult a Source

A form can produce three different results: there was no data available, the data was invalid, or there was a successful parse.

The Applicative instance will concatenate the failure messages in two FormResults.

data Enctype Source

The encoding type required by a form. The Show instance produces values that can be inserted directly into HTML.

Constructors

UrlEncoded 
Multipart 

Instances

newtype Textarea Source

A newtype wrapper around a String that converts newlines to HTML br-tags.

Constructors

Textarea 

Fields

unTextarea :: Text
 

data FieldInfo sub y Source

Using this as the intermediate XML representation for fields allows us to write generic field functions and then different functions for producing actual HTML. See, for example, fieldsToTable and fieldsToPlain.

Constructors

FieldInfo 

Instances

IsForm (FormField s m a) 
FormResult ~ formResult => IsForm (GFormMonad s m (formResult a, FieldInfo s m)) 

Utilities

Type synonyms

type Form sub y = GForm sub y (GWidget sub y ())Source

type Formlet sub y a = Maybe a -> Form sub y aSource

type FormField sub y = GForm sub y [FieldInfo sub y]Source

type FormletField sub y a = Maybe a -> FormField sub y aSource

type FormInput sub y = GForm sub y [GWidget sub y ()]Source

Unwrapping functions

generateForm :: GForm s m xml a -> GHandler s m (xml, Enctype, Html)Source

Generate a form, feeding it no data. The third element in the result tuple is a nonce hidden field.

runFormGet :: GForm s m xml a -> GHandler s m (FormResult a, xml, Enctype)Source

Run a form against GET parameters.

runFormPost :: GForm s m xml a -> GHandler s m (FormResult a, xml, Enctype, Html)Source

Run a form against POST parameters.

This function includes CSRF protection by checking a nonce value. You must therefore embed this nonce in the form as a hidden field; that is the meaning of the fourth element in the tuple.

runFormPostNoNonce :: GForm s m xml a -> GHandler s m (FormResult a, xml, Enctype)Source

Run a form against POST parameters, without CSRF protection.

runFormMonadPost :: GFormMonad s m a -> GHandler s m (a, Enctype)Source

Run a form against POST parameters. Please note that this does not provide CSRF protection.

runFormGet' :: GForm sub y xml a -> GHandler sub y aSource

Run a form against GET parameters, disregarding the resulting HTML and returning an error response on invalid input.

runFormPost' :: GForm sub y xml a -> GHandler sub y aSource

Run a form against POST parameters, disregarding the resulting HTML and returning an error response on invalid input. Note: this does not perform CSRF protection.

High-level form post unwrappers

runFormTable :: Route m -> String -> FormField s m a -> GHandler s m (FormResult a, GWidget s m ())Source

Create a table-styled form.

This function wraps around runFormPost and fieldsToTable, taking care of some of the boiler-plate in creating forms. In particular, is automatically creates the form element, sets the method, action and enctype attributes, adds the CSRF-protection nonce hidden field and inserts a submit button.

runFormDivs :: Route m -> String -> FormField s m a -> GHandler s m (FormResult a, GWidget s m ())Source

Same as runFormPostTable, but uses fieldsToDivs for styling.

Field/form helpers

fieldsToTable :: FormField sub y a -> Form sub y aSource

Display the label, tooltip, input code and errors in a single row of a table.

fieldsToDivs :: FormField sub y a -> Form sub y aSource

Display the label, tooltip, input code and errors in a single div.

fieldsToPlain :: FormField sub y a -> Form sub y aSource

Display only the actual input widget code, without any decoration.

checkForm :: (a -> FormResult b) -> GForm s m x a -> GForm s m x bSource

Add a validation check to a form.

Note that if there is a validation error, this message will not automatically appear on the form; for that, you need to use checkField.

Type classes

Template Haskell

mkToForm :: PersistEntity v => v -> Q [Dec]Source

Create ToForm instances for the given entity. In addition to regular EntityDef attributes understood by persistent, it also understands label= and tooltip=.