Commits

dp wiz  committed 2dee92d

more docs

  • Participants
  • Parent commits 3aa7de2

Comments (0)

Files changed (1)

File Layout/Bootstrap/Forms.hs

 -- Generic form builder and processor.
 -- Not-so bootstrap, but uses it's widgets.
 --
+-- Example form:
+--
+-- > checkForm = (emptyForm []) { required = [ textField "Service" "test:pass" "Service ID. For example: «test:pass»."
+-- >                                         , textField "Account" "9999995000" "Account ID. For example: «5077322496»." ]
+-- >                            , optional = [ textField "Amount" "0.12" "Amount to check." ]
+--
 -----------------------------------------------------------------------------
 
 module Layout.Bootstrap.Forms where
   simpleInput, Input(..),
   formActions, button)
 
--- | Compose a form from groups of inputs.
+-- * Form composition
+
+-- | Form container
 data Form = Form { required :: [Input]
                  , optional :: [Input]
                  , extra    :: [Input]
                                                        ! A.name "extra"
                                                        ! (A.value . toValue . title $ f))
 
+-- | Very basic form. You can add fancy required/optional fields with record modifiers
+-- or generate simple textfields from a list of names.
+emptyForm :: [T.Text] -> Form
 emptyForm extra = Form [] [] (map simpleInput extra) HM.empty HM.empty
 
+-- | Text field builder to be used with 'Form' constructor.
+textField :: T.Text -> T.Text -> T.Text -> Input
 textField title placeholder help = Input title placeholder help "" False
 
+-- * Processing with HappStack
 
--- * Happstack helpers
-
--- | Fill in form values and errors.
+-- | Fill in form values and errors using 'formData'.
+--
+-- @
+-- -- Use inside 'ServerPart':
+-- form <- lookValues checkForm
+-- @
 lookValues :: Form -> ServerPartT IO Form
 lookValues form = do
     reqd <- formData $ required form
         value <- lookTexts' $ T.unpack name
         return (name, value))
 
--- | Check if some items are missing their values.
 isRequired title = (title, ["This field is required."])
 checkRequired = concatMap (\(title, values) -> case values of
                                                    [] -> [isRequired title]