Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
Last active March 13, 2016 20:51
Show Gist options
  • Save carymrobbins/590515bb8dfb48573527 to your computer and use it in GitHub Desktop.
Save carymrobbins/590515bb8dfb48573527 to your computer and use it in GitHub Desktop.
Hack to get a nice looking checkbox field with renderBootstrap3
import Control.Applicative ((<$>), (<*>))
import Data.Text (Text)
import Text.Blaze (ToMarkup)
import Yesod
import Yesod.Form.Bootstrap3 (BootstrapFormLayout(..), renderBootstrap3, bfs)
bootstrapCheckBoxField :: (ToMarkup a, RenderMessage (HandlerSite m) FormMessage, Monad m) => a -> Field m Bool
bootstrapCheckBoxField label = checkBoxField
{ fieldView = \theId name attrs val _ -> [whamlet|\
$newline never
<div .checkbox style="margin-top: -20px; margin-bottom: 0">
<label>
<input id=#{theId} *{attrs} type="checkbox" name=#{name} value=yes :showVal id val:checked> #{label}
|]
}
where
showVal = either $ const False
-- Here's an example of using it in a form. Note that you need to pass the label directly to the field
-- instead of the field settings. An empty string needs to be passed for the field settings so the checkbox
-- can be repositioned over the label.
exampleForm = renderBootstrap3 BootstrapBasicForm $ (,)
<$> areq textField (bfs ("Text Field"::Text)) Nothing
<*> areq (bootstrapCheckBoxField ("Checked?"::Text)) "" Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment