From e1c6a6ae912782c95cf85e0cac7bc1207fb5eba6 Mon Sep 17 00:00:00 2001 From: patrick brisbin Date: Sat, 28 Jan 2012 12:51:07 -0500 Subject: [PATCH] checkBoxField, closes #245 --- yesod-form/Yesod/Form/Fields.hs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/yesod-form/Yesod/Form/Fields.hs b/yesod-form/Yesod/Form/Fields.hs index 9fa37c38..eca4be2c 100644 --- a/yesod-form/Yesod/Form/Fields.hs +++ b/yesod-form/Yesod/Form/Fields.hs @@ -25,6 +25,7 @@ module Yesod.Form.Fields , parseTime , Textarea (..) , boolField + , checkBoxField -- * File 'AForm's , fileAFormReq , fileAFormOpt @@ -384,6 +385,29 @@ boolField = Field t -> Left $ SomeMessage $ MsgInvalidBool t showVal = either (\_ -> False) +-- | While the default @'boolField'@ implements a radio button so you +-- can differentiate between an empty response (Nothing) and a no +-- response (Just False), this simpler checkbox field returns an empty +-- response as Just False. +-- +-- Note that this makes the field always optional. +-- +checkBoxField :: RenderMessage m FormMessage => Field s m Bool +checkBoxField = Field + { fieldParse = return . checkBoxParser + , fieldView = \theId name theClass val _ -> [whamlet| + +|] + } + + where + checkBoxParser [] = Right $ Just False + checkBoxParser (x:_) = case x of + "yes" -> Right $ Just True + _ -> Right $ Just False + + showVal = either (\_ -> False) + data OptionList a = OptionList { olOptions :: [Option a] , olReadExternal :: Text -> Maybe a