Merge pull request #1613 from jezen/master

Add mreqMsg
This commit is contained in:
Michael Snoyman 2019-07-18 14:52:35 +03:00 committed by GitHub
commit 37c0df8dc1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View File

@ -1,5 +1,9 @@
# ChangeLog for yesod-form # ChangeLog for yesod-form
## 1.6.6
* Added `mreqMsg` for `mreq` functionality with a configurable MsgValueRequired [#1613](https://github.com/yesodweb/yesod/pull/1613)
## 1.6.5 ## 1.6.5
* Add `.sr-only` to labels in `renderBootstrap3` when they are null. * Add `.sr-only` to labels in `renderBootstrap3` when they are null.

View File

@ -20,6 +20,7 @@ module Yesod.Form.Functions
, wreq , wreq
, wopt , wopt
, mreq , mreq
, mreqMsg
, mopt , mopt
, areq , areq
, aopt , aopt
@ -173,7 +174,24 @@ mreq :: (RenderMessage site FormMessage, HandlerSite m ~ site, MonadHandler m)
-> FieldSettings site -- ^ settings for this field -> FieldSettings site -- ^ settings for this field
-> Maybe a -- ^ optional default value -> Maybe a -- ^ optional default value
-> MForm m (FormResult a, FieldView site) -> MForm m (FormResult a, FieldView site)
mreq field fs mdef = mhelper field fs mdef (\m l -> FormFailure [renderMessage m l MsgValueRequired]) FormSuccess True mreq field fs mdef = mreqMsg field fs MsgValueRequired mdef
-- | Same as @mreq@ but with your own message to be rendered in case the value
-- is not provided.
--
-- This is useful when you have several required fields on the page and you
-- want to differentiate between which fields were left blank. Otherwise the
-- user sees "Value is required" multiple times, which is ambiguous.
--
-- @since 1.6.6
mreqMsg :: (RenderMessage site msg, HandlerSite m ~ site, MonadHandler m)
=> Field m a -- ^ form field
-> FieldSettings site -- ^ settings for this field
-> msg -- ^ Message to use in case value is Nothing
-> Maybe a -- ^ optional default value
-> MForm m (FormResult a, FieldView site)
mreqMsg field fs msg mdef = mhelper field fs mdef formFailure FormSuccess True
where formFailure m l = FormFailure [renderMessage m l msg]
-- | Converts a form field into monadic form. This field is optional, i.e. -- | Converts a form field into monadic form. This field is optional, i.e.
-- if filled in, it returns 'Just a', if left empty, it returns 'Nothing'. -- if filled in, it returns 'Just a', if left empty, it returns 'Nothing'.

View File

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