IsString instance for FormFieldSettings

This commit is contained in:
Michael Snoyman 2010-08-13 15:31:42 +03:00
parent 1a273387d6
commit cc102b67d9
2 changed files with 9 additions and 10 deletions

View File

@ -46,7 +46,6 @@ module Yesod.Form
, emailFieldProfile , emailFieldProfile
, urlFieldProfile , urlFieldProfile
, FormFieldSettings (..) , FormFieldSettings (..)
, labelSettings
-- * Pre-built fields -- * Pre-built fields
, stringField , stringField
, maybeStringField , maybeStringField
@ -107,6 +106,7 @@ import Control.Arrow ((&&&))
import qualified Text.Email.Validate as Email import qualified Text.Email.Validate as Email
import Data.List (group, sort) import Data.List (group, sort)
import Network.URI (parseURI) import Network.URI (parseURI)
import Data.String (IsString (..))
-- | A form can produce three different results: there was no data available, -- | A form can produce three different results: there was no data available,
-- the data was invalid, or there was a successful parse. -- the data was invalid, or there was a successful parse.
@ -220,6 +220,8 @@ data FormFieldSettings = FormFieldSettings
, ffsId :: Maybe String , ffsId :: Maybe String
, ffsName :: Maybe String , ffsName :: Maybe String
} }
instance IsString FormFieldSettings where
fromString s = FormFieldSettings (string s) mempty Nothing Nothing
-- | Create a required field (ie, one that cannot be blank) from a -- | Create a required field (ie, one that cannot be blank) from a
-- 'FieldProfile'.ngs -- 'FieldProfile'.ngs
@ -793,9 +795,6 @@ emailInput n =
nameSettings :: String -> FormFieldSettings nameSettings :: String -> FormFieldSettings
nameSettings n = FormFieldSettings mempty mempty (Just n) (Just n) nameSettings n = FormFieldSettings mempty mempty (Just n) (Just n)
labelSettings :: String -> FormFieldSettings
labelSettings l = FormFieldSettings (string l) mempty Nothing Nothing
textareaFieldProfile :: FieldProfile sub y String textareaFieldProfile :: FieldProfile sub y String
textareaFieldProfile = FieldProfile textareaFieldProfile = FieldProfile
{ fpParse = Right { fpParse = Right

View File

@ -54,11 +54,11 @@ getRootR = applyLayoutW $ flip wrapWidget wrapper $ do
handleFormR = do handleFormR = do
(res, form, enctype) <- runFormPost $ (,,,,,,,,,) (res, form, enctype) <- runFormPost $ (,,,,,,,,,)
<$> stringField (FormFieldSettings "My Field" "Some tooltip info" Nothing Nothing) Nothing <$> stringField (FormFieldSettings "My Field" "Some tooltip info" Nothing Nothing) Nothing
<*> stringField (labelSettings "Another field") (Just "some default text") <*> stringField ("Another field") (Just "some default text")
<*> intField (FormFieldSettings "A number field" "some nums" Nothing Nothing) (Just 5) <*> intField (FormFieldSettings "A number field" "some nums" Nothing Nothing) (Just 5)
<*> jqueryDayField (labelSettings "A day field") Nothing <*> jqueryDayField ("A day field") Nothing
<*> timeField (labelSettings "A time field") Nothing <*> timeField ("A time field") Nothing
<*> jqueryDayTimeField (labelSettings "A day/time field") Nothing <*> jqueryDayTimeField ("A day/time field") Nothing
<*> boolField FormFieldSettings <*> boolField FormFieldSettings
{ ffsLabel = "A checkbox" { ffsLabel = "A checkbox"
, ffsTooltip = "" , ffsTooltip = ""
@ -67,9 +67,9 @@ handleFormR = do
} (Just False) } (Just False)
<*> jqueryAutocompleteField AutoCompleteR <*> jqueryAutocompleteField AutoCompleteR
(FormFieldSettings "Autocomplete" "Try it!" Nothing Nothing) Nothing (FormFieldSettings "Autocomplete" "Try it!" Nothing Nothing) Nothing
<*> nicHtmlField (labelSettings "HTML") <*> nicHtmlField ("HTML")
(Just $ string "You can put rich text here") (Just $ string "You can put rich text here")
<*> maybeEmailField (labelSettings "An e-mail addres") Nothing <*> maybeEmailField ("An e-mail addres") Nothing
let mhtml = case res of let mhtml = case res of
FormSuccess (_, _, _, _, _, _, _, _, x, _) -> Just x FormSuccess (_, _, _, _, _, _, _, _, x, _) -> Just x
_ -> Nothing _ -> Nothing