Skip to content

Instantly share code, notes, and snippets.

@softmechanics
Created January 3, 2011 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save softmechanics/763039 to your computer and use it in GitHub Desktop.
Save softmechanics/763039 to your computer and use it in GitHub Desktop.
Example of Yesod's fileField
{-# LANGUAGE QuasiQuotes
, OverloadedStrings
, TypeFamilies
#-}
import Yesod
import Control.Applicative
data Test = Test
data Params = Params
{ minNumber :: Int
, maxNumber :: Int
, singleWord :: String
, pluralWord :: String
, fileTest :: FileInfo
}
deriving (Show)
paramsFormlet :: Maybe Params -> Form s m Params
-- Same as: paramsFormlet :: Formlet s m Params
paramsFormlet mparams = fieldsToTable $ Params
<$> intField "Minimum number" (fmap minNumber mparams)
<*> intField "Maximum number" (fmap maxNumber mparams)
<*> stringField "Single word" (fmap singleWord mparams)
<*> stringField "Plural word" (fmap pluralWord mparams)
<*> fileField "test"
getRootR, postRootR :: GHandler Test Test RepHtml
getRootR = postRootR
postRootR = do
(res,w,enc,nonce) <- runFormPost $ paramsFormlet Nothing
defaultLayout $ do
[$hamlet|
%form!enctype=$enc$
$nonce$
$show res$
%table
^w^
%tr
%td!colspan=2
%input!type=submit
|]
mkYesod "Test" [$parseRoutes|
/ RootR GET POST
|]
instance Yesod Test where approot _ = ""
main = basicHandler 3000 Test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment