Last active
December 18, 2015 11:09
-
-
Save xkikeg/5774196 to your computer and use it in GitHub Desktop.
Yesod Book :: Forms :: MForm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings, TypeFamilies, QuasiQuotes, | |
TemplateHaskell, MultiParamTypeClasses #-} | |
import Yesod | |
import Control.Applicative | |
import Data.Text (Text) | |
data MFormExample = MFormExample | |
mkYesod "MFormExample" [parseRoutes| | |
/ RootR GET | |
|] | |
instance Yesod MFormExample | |
instance RenderMessage MFormExample FormMessage where | |
renderMessage _ _ = defaultFormMessage | |
data Person = Person { personName :: Text, personAge :: Int } | |
deriving Show | |
personForm :: Html -> MForm Handler (FormResult Person, Widget) | |
personForm extra = do | |
(nameRes, nameView) <- mreq textField "this is not used" Nothing | |
(ageRes, ageView) <- mreq intField "neither is this" Nothing | |
let personRes = Person <$> nameRes <*> ageRes | |
let widget = do | |
toWidget [lucius| | |
##{fvId ageView} { | |
width: 3em; | |
} | |
|] | |
[whamlet| | |
#{extra} | |
<p> | |
Hello, my name is # | |
^{fvInput nameView} | |
\ and I am # | |
^{fvInput ageView} | |
\ years old. # | |
<input type=submit value="Introduce myself"> | |
|] | |
return (personRes, widget) | |
getRootR :: Handler Html | |
getRootR = do | |
((res, widget), enctype) <- runFormGet personForm | |
defaultLayout [whamlet| | |
<p>Result: #{show res} | |
<form enctype=#{enctype}> | |
^{widget} | |
|] | |
main :: IO () | |
main = warp 3000 MFormExample |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment