From f08944d888ca35898f9c233ace38f6aa727ea6b7 Mon Sep 17 00:00:00 2001 From: Jesse Kempf Date: Tue, 22 Aug 2017 13:09:33 -0700 Subject: [PATCH] Give FormResult an Alternative instance --- yesod-form/ChangeLog.md | 4 ++++ yesod-form/Yesod/Form/Types.hs | 14 +++++++++++++- yesod-form/yesod-form.cabal | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/yesod-form/ChangeLog.md b/yesod-form/ChangeLog.md index 9b0d210b..23425ea1 100644 --- a/yesod-form/ChangeLog.md +++ b/yesod-form/ChangeLog.md @@ -1,3 +1,7 @@ +## 1.4.15 + +* Added `Alternative` instance to `FormResult` to simplify handling pages with multiple forms. + ## 1.4.14 * Added `WForm` to reduce the verbosity using monadic forms. diff --git a/yesod-form/Yesod/Form/Types.hs b/yesod-form/Yesod/Form/Types.hs index b41c7b1b..1aabcc68 100644 --- a/yesod-form/Yesod/Form/Types.hs +++ b/yesod-form/Yesod/Form/Types.hs @@ -30,7 +30,7 @@ import Text.Blaze (Markup, ToMarkup (toMarkup), ToValue (toValue)) #define Html Markup #define ToHtml ToMarkup #define toHtml toMarkup -import Control.Applicative ((<$>), Applicative (..)) +import Control.Applicative ((<$>), Alternative (..), Applicative (..)) import Control.Monad (liftM) import Control.Monad.Trans.Class import Data.String (IsString (..)) @@ -45,6 +45,8 @@ import Data.Foldable -- -- The 'Applicative' instance will concatenate the failure messages in two -- 'FormResult's. +-- The 'Alternative' instance will choose 'FormFailure' before 'FormSuccess', +-- and 'FormMissing' last of all. data FormResult a = FormMissing | FormFailure [Text] | FormSuccess a @@ -80,6 +82,16 @@ instance Data.Traversable.Traversable FormResult where FormFailure errs -> pure (FormFailure errs) FormMissing -> pure FormMissing +-- | @since 1.4.15 +instance Alternative FormResult where + empty = FormMissing + + FormFailure e <|> _ = FormFailure e + _ <|> FormFailure e = FormFailure e + FormSuccess s <|> FormSuccess _ = FormSuccess s + FormMissing <|> result = result + result <|> FormMissing = result + -- | The encoding type required by a form. The 'ToHtml' instance produces values -- that can be inserted directly into HTML. data Enctype = UrlEncoded | Multipart diff --git a/yesod-form/yesod-form.cabal b/yesod-form/yesod-form.cabal index e9fba0bd..bc54653a 100644 --- a/yesod-form/yesod-form.cabal +++ b/yesod-form/yesod-form.cabal @@ -1,5 +1,5 @@ name: yesod-form -version: 1.4.14 +version: 1.4.15 license: MIT license-file: LICENSE author: Michael Snoyman