Skip to content

Commit

Permalink
Merge pull request #1340 from jprider63/master
Browse files Browse the repository at this point in the history
Fix for bug in identifyForm with empty forms.
  • Loading branch information
snoyberg committed Feb 2, 2017
2 parents 64ed079 + d59344b commit 1920604
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions yesod-form/ChangeLog.md
@@ -1,3 +1,7 @@
## 1.4.10

* Fixed `identifyForm` to properly return `FormMissing` for empty forms. [#1072](https://github.com/yesodweb/yesod/issues/1072)

## 1.4.9

* Added a `ToValue` instance for `Enctype` [#1296](https://github.com/yesodweb/yesod/pull/1296)
Expand Down
12 changes: 8 additions & 4 deletions yesod-form/Yesod/Form/Functions.hs
Expand Up @@ -62,7 +62,7 @@ import Yesod.Core
import Yesod.Core.Handler (defaultCsrfParamName)
import Network.Wai (requestMethod)
import Text.Hamlet (shamlet)
import Data.Monoid (mempty)
import Data.Monoid (mempty, (<>))
import Data.Maybe (listToMaybe, fromMaybe)
import qualified Data.Map as Map
import qualified Data.Text.Encoding as TE
Expand Down Expand Up @@ -336,21 +336,25 @@ identifyForm identVal form = \fragment -> do
-- Create hidden <input>.
let fragment' =
[shamlet|
<input type=hidden name=#{identifyFormKey} value=#{identVal}>
<input type=hidden name=#{identifyFormKey} value=identify-#{identVal}>
#{fragment}
|]

-- Check if we got its value back.
mp <- askParams
let missing = (mp >>= Map.lookup identifyFormKey) /= Just [identVal]
let missing = (mp >>= Map.lookup identifyFormKey) /= Just ["identify-" <> identVal]

-- Run the form proper (with our hidden <input>). If the
-- data is missing, then do not provide any params to the
-- form, which will turn its result into FormMissing. Also,
-- doing this avoids having lots of fields with red errors.
let eraseParams | missing = local (\(_, h, l) -> (Nothing, h, l))
| otherwise = id
eraseParams (form fragment')
( res', w) <- eraseParams (form fragment')

-- Empty forms now properly return FormMissing. [#1072](https://github.com/yesodweb/yesod/issues/1072)
let res = if missing then FormMissing else res'
return ( res, w)

identifyFormKey :: Text
identifyFormKey = "_formid"
Expand Down
2 changes: 1 addition & 1 deletion yesod-form/yesod-form.cabal
@@ -1,5 +1,5 @@
name: yesod-form
version: 1.4.9
version: 1.4.10
license: MIT
license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com>
Expand Down

0 comments on commit 1920604

Please sign in to comment.