From d15368065febdfc40e5f87d9a0e5b3e972e11e69 Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Mon, 22 Nov 2010 07:47:53 +0200 Subject: [PATCH] passwordField --- Yesod/Form/Fields.hs | 10 ++++++++++ Yesod/Form/Profiles.hs | 15 +++++++++++++++ hellowidget.hs | 7 ++++--- yesod.cabal | 2 +- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/Yesod/Form/Fields.hs b/Yesod/Form/Fields.hs index abdfc4b0..a71bd576 100644 --- a/Yesod/Form/Fields.hs +++ b/Yesod/Form/Fields.hs @@ -6,6 +6,7 @@ module Yesod.Form.Fields ( -- * Fields -- ** Required stringField + , passwordField , textareaField , hiddenField , intField @@ -20,6 +21,7 @@ module Yesod.Form.Fields , fileField -- ** Optional , maybeStringField + , maybePasswordField , maybeTextareaField , maybeHiddenField , maybeIntField @@ -65,6 +67,14 @@ maybeStringField :: (IsForm f, FormType f ~ Maybe String) => FormFieldSettings -> Maybe (Maybe String) -> f maybeStringField = optionalFieldHelper stringFieldProfile +passwordField :: (IsForm f, FormType f ~ String) + => FormFieldSettings -> Maybe String -> f +passwordField = requiredFieldHelper passwordFieldProfile + +maybePasswordField :: (IsForm f, FormType f ~ Maybe String) + => FormFieldSettings -> Maybe (Maybe String) -> f +maybePasswordField = optionalFieldHelper passwordFieldProfile + intInput :: Integral i => String -> FormInput sub master i intInput n = mapFormXml fieldsToInput $ diff --git a/Yesod/Form/Profiles.hs b/Yesod/Form/Profiles.hs index fa7e16c5..b42bf3c7 100644 --- a/Yesod/Form/Profiles.hs +++ b/Yesod/Form/Profiles.hs @@ -4,6 +4,7 @@ {-# LANGUAGE CPP #-} module Yesod.Form.Profiles ( stringFieldProfile + , passwordFieldProfile , textareaFieldProfile , hiddenFieldProfile , intFieldProfile @@ -166,6 +167,20 @@ stringFieldProfile = FieldProfile |] } +passwordFieldProfile :: FieldProfile s m String +passwordFieldProfile = FieldProfile + { fpParse = Right + , fpRender = id + , fpWidget = \theId name val isReq -> addHamlet +#if GHC7 + [hamlet| +#else + [$hamlet| +#endif +%input#$theId$!name=$name$!type=password!:isReq:required!value=$val$ +|] + } + readMay :: Read a => String -> Maybe a readMay s = case reads s of (x, _):_ -> Just x diff --git a/hellowidget.hs b/hellowidget.hs index 038768f0..3c73e81a 100644 --- a/hellowidget.hs +++ b/hellowidget.hs @@ -61,7 +61,7 @@ getRootR = defaultLayout $ wrapper $ do addHtmlHead [$hamlet|%meta!keywords=haskell|] handleFormR = do - (res, form, enctype, hidden) <- runFormPost $ fieldsToTable $ (,,,,,,,,,,) + (res, form, enctype, hidden) <- runFormPost $ fieldsToTable $ (,,,,,,,,,,,) <$> stringField (FormFieldSettings "My Field" "Some tooltip info" Nothing Nothing) Nothing <*> stringField ("Another field") (Just "some default text") <*> intField (FormFieldSettings "A number field" "some nums" Nothing Nothing) (Just 5) @@ -85,11 +85,12 @@ handleFormR = do <*> maybeEmailField ("An e-mail addres") Nothing <*> maybeTextareaField "A text area" Nothing <*> maybeFileField "Any file" + <*> maybePasswordField "Enter a password" Nothing let (mhtml, mfile) = case res of - FormSuccess (_, _, _, _, _, _, _, x, _, _, y) -> (Just x, y) + FormSuccess (_, _, _, _, _, _, _, x, _, _, y, _) -> (Just x, y) _ -> (Nothing, Nothing) let txt = case res of - FormSuccess (_, _, _, _, _, _, _, _, _, Just x, _) -> Just x + FormSuccess (_, _, _, _, _, _, _, _, _, Just x, _, _) -> Just x _ -> Nothing defaultLayout $ do addCassius [$cassius| diff --git a/yesod.cabal b/yesod.cabal index efdfe931..61a68b76 100644 --- a/yesod.cabal +++ b/yesod.cabal @@ -1,5 +1,5 @@ name: yesod -version: 0.6.3.1 +version: 0.6.4 license: BSD3 license-file: LICENSE author: Michael Snoyman