Invalid input into a field would not repopulate.

Like if you put "asdf" into an int field, the page would not refresh
with the errant "asdf" in it, because asdf can't be represented as an
int.
This commit is contained in:
DavidM 2011-06-21 18:26:59 -04:00
parent d2becab26a
commit 1421bacd49
5 changed files with 28 additions and 26 deletions

View File

@ -128,7 +128,7 @@ intField = Field
|] |]
} }
where where
showVal = maybe "" (pack . showI) showVal = either id (pack . showI)
showI x = show (fromIntegral x :: Integer) showI x = show (fromIntegral x :: Integer)
doubleField :: Monad monad => Field (GGWidget master monad ()) FormMessage Double doubleField :: Monad monad => Field (GGWidget master monad ()) FormMessage Double
@ -143,7 +143,7 @@ doubleField = Field
<input id="#{theId}" name="#{name}" type="text" :isReq:required="" value="#{showVal val}"> <input id="#{theId}" name="#{name}" type="text" :isReq:required="" value="#{showVal val}">
|] |]
} }
where showVal = maybe "" (pack . show) where showVal = either id (pack . show)
dayField :: Monad monad => Field (GGWidget master monad ()) FormMessage Day dayField :: Monad monad => Field (GGWidget master monad ()) FormMessage Day
dayField = Field dayField = Field
@ -153,7 +153,7 @@ dayField = Field
<input id="#{theId}" name="#{name}" type="date" :isReq:required="" value="#{showVal val}"> <input id="#{theId}" name="#{name}" type="date" :isReq:required="" value="#{showVal val}">
|] |]
} }
where showVal = maybe "" (pack . show) where showVal = either id (pack . show)
timeField :: Monad monad => Field (GGWidget master monad ()) FormMessage TimeOfDay timeField :: Monad monad => Field (GGWidget master monad ()) FormMessage TimeOfDay
timeField = Field timeField = Field
@ -164,7 +164,7 @@ timeField = Field
|] |]
} }
where where
showVal = maybe "" (pack . show . roundFullSeconds) showVal = either id (pack . show . roundFullSeconds)
roundFullSeconds tod = roundFullSeconds tod =
TimeOfDay (todHour tod) (todMin tod) fullSec TimeOfDay (todHour tod) (todMin tod) fullSec
where where
@ -178,7 +178,7 @@ htmlField = Field
<textarea id="#{theId}" name="#{name}" .html>#{showVal val} <textarea id="#{theId}" name="#{name}" .html>#{showVal val}
|] |]
} }
where showVal = maybe "" (pack . renderHtml) where showVal = either id (pack . renderHtml)
-- | A newtype wrapper around a 'String' that converts newlines to HTML -- | A newtype wrapper around a 'String' that converts newlines to HTML
-- br-tags. -- br-tags.
@ -203,7 +203,7 @@ textareaField = Field
{ fieldParse = blank $ Right . Textarea { fieldParse = blank $ Right . Textarea
, fieldView = \theId name val _isReq -> addHamlet , fieldView = \theId name val _isReq -> addHamlet
[HAMLET|\ [HAMLET|\
<textarea id="#{theId}" name="#{name}">#{maybe "" unTextarea val} <textarea id="#{theId}" name="#{name}">#{either id unTextarea val}
|] |]
} }
@ -212,7 +212,7 @@ hiddenField = Field
{ fieldParse = blank $ Right { fieldParse = blank $ Right
, fieldView = \theId name val _isReq -> addHamlet , fieldView = \theId name val _isReq -> addHamlet
[HAMLET|\ [HAMLET|\
<input type="hidden" id="#{theId}" name="#{name}" value="#{maybe "" id val}"> <input type="hidden" id="#{theId}" name="#{name}" value="#{either id id val}">
|] |]
} }
@ -221,7 +221,7 @@ textField = Field
{ fieldParse = blank $ Right { fieldParse = blank $ Right
, fieldView = \theId name val isReq -> , fieldView = \theId name val isReq ->
[WHAMLET| [WHAMLET|
<input id="#{theId}" name="#{name}" type="text" :isReq:required value="#{maybe "" id val}"> <input id="#{theId}" name="#{name}" type="text" :isReq:required value="#{either id id val}">
|] |]
} }
@ -230,7 +230,7 @@ passwordField = Field
{ fieldParse = blank $ Right { fieldParse = blank $ Right
, fieldView = \theId name val isReq -> addHamlet , fieldView = \theId name val isReq -> addHamlet
[HAMLET|\ [HAMLET|\
<input id="#{theId}" name="#{name}" type="password" :isReq:required="" value="#{maybe "" id val}"> <input id="#{theId}" name="#{name}" type="password" :isReq:required="" value="#{either id id val}">
|] |]
} }
@ -280,7 +280,7 @@ emailField = Field
else Left $ MsgInvalidEmail s else Left $ MsgInvalidEmail s
, fieldView = \theId name val isReq -> addHamlet , fieldView = \theId name val isReq -> addHamlet
[HAMLET|\ [HAMLET|\
<input id="#{theId}" name="#{name}" type="email" :isReq:required="" value="#{maybe "" id val}"> <input id="#{theId}" name="#{name}" type="email" :isReq:required="" value="#{either id id val}">
|] |]
} }
@ -290,7 +290,7 @@ searchField autoFocus = Field
{ fieldParse = blank Right { fieldParse = blank Right
, fieldView = \theId name val isReq -> do , fieldView = \theId name val isReq -> do
addHtml [HAMLET|\ addHtml [HAMLET|\
<input id="#{theId}" name="#{name}" type="search" :isReq:required="" :autoFocus:autofocus="" value="#{maybe "" id val}"> <input id="#{theId}" name="#{name}" type="search" :isReq:required="" :autoFocus:autofocus="" value="#{either id id val}">
|] |]
when autoFocus $ do when autoFocus $ do
addHtml $ [HAMLET|\<script>if (!('autofocus' in document.createElement('input'))) {document.getElementById('#{theId}').focus();}</script> addHtml $ [HAMLET|\<script>if (!('autofocus' in document.createElement('input'))) {document.getElementById('#{theId}').focus();}</script>
@ -309,7 +309,7 @@ urlField = Field
Just _ -> Right s Just _ -> Right s
, fieldView = \theId name val isReq -> addHtml , fieldView = \theId name val isReq -> addHtml
[HAMLET| [HAMLET|
<input ##{theId} name=#{name} type=url :isReq:required value=#{maybe "" id val}> <input ##{theId} name=#{name} type=url :isReq:required value=#{either id id val}>
|] |]
} }
@ -347,10 +347,10 @@ boolField = Field
<label for=#{theId}-none>_{MsgSelectNone} <label for=#{theId}-none>_{MsgSelectNone}
<input id=#{theId}-yes type=radio name=#{name} value=yes :maybe False id val:checked> <input id=#{theId}-yes type=radio name=#{name} value=yes :showVal id val:checked>
<label for=#{theId}-yes>_{MsgBoolYes} <label for=#{theId}-yes>_{MsgBoolYes}
<input id=#{theId}-no type=radio name=#{name} value=no :maybe False not val:checked> <input id=#{theId}-no type=radio name=#{name} value=no :showVal not val:checked>
<label for=#{theId}-no>_{MsgBoolNo} <label for=#{theId}-no>_{MsgBoolNo}
|] |]
} }
@ -362,6 +362,7 @@ boolField = Field
"yes" -> Right $ Just True "yes" -> Right $ Just True
"no" -> Right $ Just False "no" -> Right $ Just False
t -> Left $ MsgInvalidBool t t -> Left $ MsgInvalidBool t
showVal = either (\_ -> False)
multiSelectFieldHelper :: (Show a, Eq a, Monad monad) multiSelectFieldHelper :: (Show a, Eq a, Monad monad)
=> (Text -> Text -> GGWidget master monad () -> GGWidget master monad ()) => (Text -> Text -> GGWidget master monad () -> GGWidget master monad ())
@ -375,7 +376,7 @@ multiSelectFieldHelper outside inside opts = Field
theId theId
name name
(pack $ show $ fst pair) (pack $ show $ fst pair)
((fst pair) `elem` (maybe [] selectedVals vals)) ((fst pair) `elem` (either (\_ -> []) selectedVals vals)) -- We are presuming that select fields can't hold invalid values
(fst $ snd pair) (fst $ snd pair)
} }
where where
@ -406,8 +407,8 @@ selectFieldHelper outside onOpt inside opts = Field
where where
pairs = zip [1 :: Int ..] opts -- FIXME use IntMap pairs = zip [1 :: Int ..] opts -- FIXME use IntMap
rpairs = zip (map snd opts) [1 :: Int ..] rpairs = zip (map snd opts) [1 :: Int ..]
render Nothing = "" render (Left _) = ""
render (Just a) = maybe "" (pack . show) $ lookup a rpairs render (Right a) = maybe "" (pack . show) $ lookup a rpairs
selectParser [] = Right Nothing selectParser [] = Right Nothing
selectParser (s:_) = case s of selectParser (s:_) = case s of
"" -> Right Nothing "" -> Right Nothing

View File

@ -40,6 +40,7 @@ import Yesod.Request (reqNonce, reqWaiRequest, reqGetParams, languages)
import Network.Wai (requestMethod) import Network.Wai (requestMethod)
import Text.Hamlet.NonPoly (html) import Text.Hamlet.NonPoly (html)
import Data.Monoid (mempty) import Data.Monoid (mempty)
import Data.Maybe (listToMaybe)
import Yesod.Message (RenderMessage (..)) import Yesod.Message (RenderMessage (..))
#if __GLASGOW_HASKELL__ >= 700 #if __GLASGOW_HASKELL__ >= 700
@ -112,15 +113,15 @@ mhelper Field {..} FieldSettings {..} mdef onMissing onFound isReq = do
let mr2 = renderMessage master langs let mr2 = renderMessage master langs
let (res, val) = let (res, val) =
case mp of case mp of
Nothing -> (FormMissing, mdef) Nothing -> (FormMissing, maybe (Left "") Right mdef)
Just p -> Just p ->
let mvals = map snd $ filter (\(n,_) -> n == name) p let mvals = map snd $ filter (\(n,_) -> n == name) p
in case fieldParse mvals of in case fieldParse mvals of
Left e -> (FormFailure [renderMessage master langs e], Nothing) -- There is no way to retain the wrong value Left e -> (FormFailure [renderMessage master langs e], maybe (Left "") Left (listToMaybe mvals))
Right mx -> Right mx ->
case mx of case mx of
Nothing -> (onMissing master langs, Nothing) Nothing -> (onMissing master langs, Left "")
Just x -> (onFound x, Just x) Just x -> (onFound x, Right x)
return (res, FieldView return (res, FieldView
{ fvLabel = toHtml $ mr2 fsLabel { fvLabel = toHtml $ mr2 fsLabel
, fvTooltip = fmap toHtml $ fmap mr2 fsTooltip , fvTooltip = fmap toHtml $ fmap mr2 fsTooltip

View File

@ -93,7 +93,7 @@ $(function(){$("##{theId}").datepicker({
|] |]
} }
where where
showVal = maybe "" (pack . show) showVal = either id (pack . show)
jsBool True = "true" :: Text jsBool True = "true" :: Text
jsBool False = "false" :: Text jsBool False = "false" :: Text
mos (Left i) = show i mos (Left i) = show i
@ -139,7 +139,7 @@ $(function(){$("##{theId}").datetimepicker({dateFormat : "yyyy/mm/dd h:MM TT"})}
|] |]
} }
where where
showVal = maybe "" (pack . jqueryDayTimeUTCTime) showVal = either id (pack . jqueryDayTimeUTCTime)
parseUTCTime :: String -> Either FormMessage UTCTime parseUTCTime :: String -> Either FormMessage UTCTime
parseUTCTime s = parseUTCTime s =
@ -156,7 +156,7 @@ jqueryAutocompleteField src = Field
{ fieldParse = blank $ Right { fieldParse = blank $ Right
, fieldView = \theId name val isReq -> do , fieldView = \theId name val isReq -> do
addHtml [HAMLET|\ addHtml [HAMLET|\
<input id="#{theId}" name="#{name}" type="text" :isReq:required="" value="#{maybe "" id val}" .autocomplete> <input id="#{theId}" name="#{name}" type="text" :isReq:required="" value="#{either id id val}" .autocomplete>
|] |]
addScript' urlJqueryJs addScript' urlJqueryJs
addScript' urlJqueryUiJs addScript' urlJqueryUiJs

View File

@ -56,7 +56,7 @@ bkLib.onDomLoaded(function(){new nicEditor({fullPanel:true}).panelInstance("#{th
|] |]
} }
where where
showVal = maybe "" (pack . renderHtml) showVal = either id (pack . renderHtml)
addScript' :: (y -> Either (Route y) Text) -> GWidget sub y () addScript' :: (y -> Either (Route y) Text) -> GWidget sub y ()
addScript' f = do addScript' f = do

View File

@ -117,7 +117,7 @@ data Field xml msg a = Field
{ fieldParse :: [Text] -> Either msg (Maybe a) { fieldParse :: [Text] -> Either msg (Maybe a)
, fieldView :: Text -- ^ ID , fieldView :: Text -- ^ ID
-> Text -- ^ name -> Text -- ^ name
-> Maybe a -- ^ value -> Either Text a -- ^ value could be invalid text or a legitimate a
-> Bool -- ^ required? -> Bool -- ^ required?
-> xml -> xml
} }