diff --git a/Yesod/Form/Fields.hs b/Yesod/Form/Fields.hs index db11563d..741ecbb9 100644 --- a/Yesod/Form/Fields.hs +++ b/Yesod/Form/Fields.hs @@ -17,6 +17,7 @@ module Yesod.Form.Fields , emailField , searchField , selectField + , multiSelectField , AutoFocus , urlField , doubleField @@ -40,6 +41,9 @@ import Network.URI (parseURI) import Database.Persist (PersistField) import Text.HTML.SanitizeXSS (sanitizeBalance) import Control.Monad (when, unless) +import Data.List (intersect, nub) +import Data.Either (rights) +import Data.Maybe (catMaybes) import qualified Blaze.ByteString.Builder.Html.Utf8 as B import Blaze.ByteString.Builder (writeByteString, toLazyByteString) @@ -104,10 +108,12 @@ defaultFormMessage (MsgInvalidBool t) = "Invalid boolean: " `mappend` t defaultFormMessage MsgBoolYes = "Yes" defaultFormMessage MsgBoolNo = "No" -blank :: (Text -> Either msg a) -> Maybe Text -> Either msg (Maybe a) -blank _ Nothing = Right Nothing -blank _ (Just "") = Right Nothing -blank f (Just t) = either Left (Right . Just) $ f t +blank :: (Text -> Either msg a) -> [Text] -> Either msg (Maybe a) +blank _ [] = Right Nothing +blank _ ("":_) = Right Nothing +blank f (x:_) = either Left (Right . Just) $ f x + + intField :: (Monad monad, Integral i) => Field (GGWidget master monad ()) FormMessage i intField = Field @@ -115,13 +121,14 @@ intField = Field case Data.Text.Read.signed Data.Text.Read.decimal s of Right (a, "") -> Right a _ -> Left $ MsgInvalidInteger s - , fieldRender = pack . showI + , fieldView = \theId name val isReq -> addHamlet [HAMLET|\ - + |] } where + showVal = either id (pack . showI) showI x = show (fromIntegral x :: Integer) doubleField :: Monad monad => Field (GGWidget master monad ()) FormMessage Double @@ -130,33 +137,34 @@ doubleField = Field case Data.Text.Read.double s of Right (a, "") -> Right a _ -> Left $ MsgInvalidNumber s - , fieldRender = pack . show + , fieldView = \theId name val isReq -> addHamlet [HAMLET|\ - + |] } + where showVal = either id (pack . show) dayField :: Monad monad => Field (GGWidget master monad ()) FormMessage Day dayField = Field { fieldParse = blank $ parseDate . unpack - , fieldRender = pack . show , fieldView = \theId name val isReq -> addHamlet [HAMLET|\ - + |] } + where showVal = either id (pack . show) timeField :: Monad monad => Field (GGWidget master monad ()) FormMessage TimeOfDay timeField = Field { fieldParse = blank $ parseTime . unpack - , fieldRender = pack . show . roundFullSeconds , fieldView = \theId name val isReq -> addHamlet [HAMLET|\ - + |] } where + showVal = either id (pack . show . roundFullSeconds) roundFullSeconds tod = TimeOfDay (todHour tod) (todMin tod) fullSec where @@ -165,12 +173,12 @@ timeField = Field htmlField :: Monad monad => Field (GGWidget master monad ()) FormMessage Html htmlField = Field { fieldParse = blank $ Right . preEscapedString . sanitizeBalance . unpack -- FIXME make changes to xss-sanitize - , fieldRender = pack . renderHtml , fieldView = \theId name val _isReq -> addHamlet [HAMLET|\ -