diff --git a/Yesod/Form/Fields.hs b/Yesod/Form/Fields.hs index db11563d..9fedf1b2 100644 --- a/Yesod/Form/Fields.hs +++ b/Yesod/Form/Fields.hs @@ -104,10 +104,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 +117,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 = maybe "" (pack . showI) showI x = show (fromIntegral x :: Integer) doubleField :: Monad monad => Field (GGWidget master monad ()) FormMessage Double @@ -130,33 +133,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 = maybe "" (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 = maybe "" (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 = maybe "" (pack . show . roundFullSeconds) roundFullSeconds tod = TimeOfDay (todHour tod) (todMin tod) fullSec where @@ -165,12 +169,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|\ -