Add mreqMsg
When a form has multiple `mreq` fields and the user submits a form with more than one of these values missing, they see an ambiguous collection of "Value is required" messages. The `mreqMsg` function allows you to provide your own MsgValueRequired message for a better UX.
This commit is contained in:
parent
bc73850173
commit
14ade1ad98
3 changed files with 23 additions and 1 deletions
|
@ -1,5 +1,9 @@
|
|||
# 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
|
||||
|
||||
* Add `.sr-only` to labels in `renderBootstrap3` when they are null.
|
||||
|
|
|
@ -20,6 +20,7 @@ module Yesod.Form.Functions
|
|||
, wreq
|
||||
, wopt
|
||||
, mreq
|
||||
, mreqMsg
|
||||
, mopt
|
||||
, areq
|
||||
, aopt
|
||||
|
@ -175,6 +176,23 @@ mreq :: (RenderMessage site FormMessage, HandlerSite m ~ site, MonadHandler m)
|
|||
-> MForm m (FormResult a, FieldView site)
|
||||
mreq field fs mdef = mhelper field fs mdef (\m l -> FormFailure [renderMessage m l MsgValueRequired]) FormSuccess True
|
||||
|
||||
-- | 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.
|
||||
-- if filled in, it returns 'Just a', if left empty, it returns 'Nothing'.
|
||||
-- Arguments are the same as for 'mreq' (apart from type of default value).
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
name: yesod-form
|
||||
version: 1.6.5
|
||||
version: 1.6.6
|
||||
license: MIT
|
||||
license-file: LICENSE
|
||||
author: Michael Snoyman <michael@snoyman.com>
|
||||
|
|
Loading…
Reference in a new issue