Skip to content

Commit

Permalink
Create checkboxesField' - correct version of checkboxesField
Browse files Browse the repository at this point in the history
  • Loading branch information
ktak-007 committed Aug 19, 2024
1 parent 7eb3c1e commit 2e66ff9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions yesod-form/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog for yesod-form

## 1.7.9

* Added `checkboxesField'` for creating checkbox in more correct wat than original `checkboxesField`
Function `checkboxesField` marked as deprecated.

## 1.7.8

Expand Down
22 changes: 22 additions & 0 deletions yesod-form/Yesod/Form/Fields.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module Yesod.Form.Fields
, radioFieldList
, withRadioField
, checkboxesField
, checkboxesField'
, checkboxesFieldList
, multiSelectField
, multiSelectFieldList
Expand Down Expand Up @@ -125,6 +126,7 @@ import Data.Monoid
import Data.Char (isHexDigit)

{-# DEPRECATED radioField "This function seems to have a bug (label could not be found with byLabel algorithm)" #-}
{-# DEPRECATED checkboxesField "This function seems to have a bug (label could not be found with byLabel algorithm)" #-}

defaultFormMessage :: FormMessage -> Text
defaultFormMessage = englishFormMessage
Expand Down Expand Up @@ -533,6 +535,26 @@ checkboxesField ioptlist = (multiSelectField ioptlist)
|]
}

-- | Creates an input with @type="checkbox"@ for selecting multiple options.
checkboxesField' :: Eq a
=> HandlerFor site (OptionList a)
-> Field (HandlerFor site) [a]
checkboxesField' ioptlist = (multiSelectField ioptlist)
{ fieldView =
\theId name attrs val _isReq -> do
opts <- olOptions <$> handlerToWidget ioptlist
let optselected (Left _) _ = False
optselected (Right vals) opt = optionInternalValue opt `elem` vals
[whamlet|
<span ##{theId}>
$forall opt <- opts
<input id=#{theId}-#{optionExternalValue opt} type=checkbox name=#{name} value=#{optionExternalValue opt} *{attrs} :optselected val opt:checked>
<label for=#{theId}-#{optionExternalValue opt}>
#{optionDisplay opt}
|]
}


-- | Creates an input with @type="radio"@ for selecting one option.
radioField :: (Eq a, RenderMessage site FormMessage)
=> HandlerFor site (OptionList a)
Expand Down
2 changes: 1 addition & 1 deletion yesod-form/yesod-form.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: >= 1.10
name: yesod-form
version: 1.7.8
version: 1.7.9
license: MIT
license-file: LICENSE
author: Michael Snoyman <[email protected]>
Expand Down

0 comments on commit 2e66ff9

Please sign in to comment.