checkField

This commit is contained in:
Michael Snoyman 2010-10-07 23:34:02 +02:00
parent 7e95ce974d
commit 7377919f6c
2 changed files with 23 additions and 2 deletions

View File

@ -190,7 +190,7 @@ optionalFieldHelper (FieldProfile parse render mkWidget) ffs orig' = GForm $ do
, fiName = name
, fiInput = mkWidget theId name val False
, fiErrors = case res of
FormFailure [x] -> Just $ string x
FormFailure x -> Just $ string $ unlines x
_ -> Nothing
}
return (res, [fi], UrlEncoded)
@ -241,6 +241,7 @@ type FormField sub y = GForm sub y [FieldInfo sub y]
type FormletField sub y a = Maybe a -> FormField sub y a
type FormInput sub y = GForm sub y [GWidget sub y ()]
-- | FIXME Add some docs, especially about how failures from this function don't show up in the HTML.
checkForm :: (a -> FormResult b) -> GForm s m x a -> GForm s m x b
checkForm f (GForm form) = GForm $ do
(res, xml, enc) <- form
@ -250,6 +251,26 @@ checkForm f (GForm form) = GForm $ do
FormMissing -> FormMissing
return (res', xml, enc)
checkField :: (a -> Either String b) -> FormField s m a -> FormField s m b
checkField f (GForm form) = GForm $ do
(res, xml, enc) <- form
let (res', merr) =
case res of
FormSuccess a ->
case f a of
Left e -> (FormFailure [e], Just e)
Right x -> (FormSuccess x, Nothing)
let xml' =
case merr of
Nothing -> xml
Just err -> flip map xml $ \fi -> fi
{ fiErrors = Just $
case fiErrors fi of
Nothing -> string err
Just x -> x
}
return (res', xml', enc)
askParams :: Monad m => StateT Ints (ReaderT Env m) Env
askParams = lift ask

View File

@ -25,7 +25,7 @@ library
, time >= 1.1.4 && < 1.3
, wai >= 0.2.0 && < 0.3
, wai-extra >= 0.2.2 && < 0.3
, authenticate >= 0.6.3.2 && < 0.7
, authenticate >= 0.7 && < 0.8
, bytestring >= 0.9.1.4 && < 0.10
, directory >= 1 && < 1.2
, text >= 0.5 && < 0.10