{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE NoMonomorphismRestriction #-} -- FIXME remove module Yesod.Form.Fields ( -- * Fields -- ** Required stringField , passwordField , textareaField , hiddenField , intField , doubleField , dayField , timeField , htmlField , selectField , radioField , boolField , emailField , searchField , urlField , fileField -- ** Optional , maybeStringField , maybePasswordField , maybeTextareaField , maybeHiddenField , maybeIntField , maybeDoubleField , maybeDayField , maybeTimeField , maybeHtmlField , maybeSelectField , maybeRadioField , maybeEmailField , maybeSearchField , maybeUrlField , maybeFileField {- FIXME -- * Inputs -- ** Required , stringInput , intInput , boolInput , dayInput , emailInput , urlInput -- ** Optional , maybeStringInput , maybeDayInput , maybeIntInput -} ) where import Yesod.Form.Core import Yesod.Form.Profiles import Yesod.Request (FileInfo) import Yesod.Widget (GWidget) import Control.Monad.Trans.Class (lift) import Control.Monad.Trans.Reader (ask) import Data.Time (Day, TimeOfDay) import Text.Hamlet import Data.Monoid import Control.Monad (join) import Data.Maybe (fromMaybe, isNothing) import Data.Text (Text, unpack) import qualified Data.Text as T #if __GLASGOW_HASKELL__ >= 700 #define HAMLET hamlet #else #define HAMLET $hamlet #endif stringField = requiredFieldHelper stringFieldProfile maybeStringField = optionalFieldHelper stringFieldProfile passwordField = requiredFieldHelper passwordFieldProfile maybePasswordField = optionalFieldHelper passwordFieldProfile {- FIXME intInput n = mapFormXml fieldsToInput $ requiredFieldHelper intFieldProfile (nameSettings n) Nothing maybeIntInput n = mapFormXml fieldsToInput $ optionalFieldHelper intFieldProfile (nameSettings n) Nothing -} intField = requiredFieldHelper intFieldProfile maybeIntField = optionalFieldHelper intFieldProfile doubleField = requiredFieldHelper doubleFieldProfile maybeDoubleField = optionalFieldHelper doubleFieldProfile dayField = requiredFieldHelper dayFieldProfile maybeDayField = optionalFieldHelper dayFieldProfile timeField = requiredFieldHelper timeFieldProfile maybeTimeField = optionalFieldHelper timeFieldProfile boolField ffs orig = do env <- askParams let label = ffsLabel ffs tooltip = ffsTooltip ffs name <- maybe newFormIdent return $ ffsName ffs theId <- maybe newFormIdent return $ ffsId ffs let (res, val) = if null env then (FormMissing, fromMaybe False orig) else case lookup name env of Nothing -> (FormSuccess False, False) Just "" -> (FormSuccess False, False) Just "false" -> (FormSuccess False, False) Just _ -> (FormSuccess True, True) let fi = FieldInfo { fiLabel = toHtml label , fiTooltip = tooltip , fiIdent = theId , fiInput = [HAMLET| |] , fiErrors = case res of FormFailure [x] -> Just $ toHtml x _ -> Nothing , fiRequired = True } return (res, fi, UrlEncoded) htmlField = requiredFieldHelper htmlFieldProfile maybeHtmlField = optionalFieldHelper htmlFieldProfile selectField pairs ffs initial = do env <- askParams let label = ffsLabel ffs tooltip = ffsTooltip ffs theId <- maybe newFormIdent return $ ffsId ffs name <- maybe newFormIdent return $ ffsName ffs let pairs' = zip [1 :: Int ..] pairs let res = case lookup name env of Nothing -> FormMissing Just "none" -> FormFailure ["Field is required"] Just x -> case reads $ unpack x of (x', _):_ -> case lookup x' pairs' of Nothing -> FormFailure ["Invalid entry"] Just (y, _) -> FormSuccess y [] -> FormFailure ["Invalid entry"] let isSelected x = case res of FormSuccess y -> x == y _ -> Just x == initial let input = #if __GLASGOW_HASKELL__ >= 700 [hamlet| #else [$hamlet| #endif