Merge pull request #1433 from binarysunrise-io/form-result-instantiate-alternative

Give FormResult an Alternative instance
This commit is contained in:
Michael Snoyman 2017-08-23 08:44:59 +03:00 committed by GitHub
commit a1c7fc5281
3 changed files with 18 additions and 2 deletions

View File

@ -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.

View File

@ -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

View File

@ -1,5 +1,5 @@
name: yesod-form
version: 1.4.14
version: 1.4.15
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>