From 0b5b132fa354fa6d51bff9b1043c5f720b07c346 Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Mon, 15 Nov 2010 22:36:37 +0200 Subject: [PATCH] runFormPostTable and runFormPostDivs --- Yesod/Form.hs | 39 +++++++++++++++++++++++++++++++++++++++ yesod.cabal | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Yesod/Form.hs b/Yesod/Form.hs index cb28907c..5d0aeabb 100644 --- a/Yesod/Form.hs +++ b/Yesod/Form.hs @@ -29,6 +29,9 @@ module Yesod.Form , runFormMonadPost , runFormGet' , runFormPost' + -- ** High-level form post unwrappers + , runFormPostTable + , runFormPostDivs -- * Field/form helpers , fieldsToTable , fieldsToDivs @@ -45,6 +48,7 @@ import Yesod.Form.Core import Yesod.Form.Fields import Yesod.Form.Class import Yesod.Form.Profiles (Textarea (..)) +import Yesod.Widget (GWidget) import Text.Hamlet import Yesod.Request @@ -144,6 +148,41 @@ runFormPost' f = do x <- runFormGeneric pp files f helper x +-- | 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. +runFormPostTable :: Route m -> String -> FormField s m a + -> GHandler s m (FormResult a, GWidget s m ()) +runFormPostTable dest inputLabel form = do + (res, widget, enctype, nonce) <- runFormPost $ fieldsToTable form + let widget' = [$hamlet| +%form!method=post!action=@dest@!enctype=$enctype$ + %table + ^widget^ + %tr + %td!colspan=2 + $nonce$ + %input!type=submit!value=$inputLabel$ +|] + return (res, widget') + +-- | Same as 'runFormPostTable', but uses 'fieldsToDivs' for styling. +runFormPostDivs :: Route m -> String -> FormField s m a + -> GHandler s m (FormResult a, GWidget s m ()) +runFormPostDivs dest inputLabel form = do + (res, widget, enctype, nonce) <- runFormPost $ fieldsToDivs form + let widget' = [$hamlet| +%form!method=post!action=@dest@!enctype=$enctype$ + ^widget^ + %div + $nonce$ + %input!type=submit!value=$inputLabel$ +|] + return (res, widget') + -- | Run a form against GET parameters, disregarding the resulting HTML and -- returning an error response on invalid input. runFormGet' :: GForm sub y xml a -> GHandler sub y a diff --git a/yesod.cabal b/yesod.cabal index 33bf98e9..0b8deb1d 100644 --- a/yesod.cabal +++ b/yesod.cabal @@ -1,5 +1,5 @@ name: yesod -version: 0.6.1.3 +version: 0.6.2 license: BSD3 license-file: LICENSE author: Michael Snoyman