Scotty backend for the digestive-functors library
Haskell
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src/Text/Digestive
LICENSE
README.md
Setup.hs
digestive-functors-scotty.cabal

README.md

{-# LANGUAGE OverloadedStrings #-}

import Web.Scotty

import Text.Digestive
import Text.Digestive.Scotty

import Control.Applicative ((<$>), (<*>))

data FormData = FormData { field1 :: String
                         , field2 :: Integer
                         }

testForm :: Monad m => Form String m FormData
testForm = FormData
           <$> "field1" .: string Nothing
           <*> "field2" .: stringRead "read failed" Nothing

main :: IO ()
main = scotty 3000 $ do
    get "/" $ do
        setHeader "Content-Type" "text/html"
        raw " <html><body><form enctype='multipart/form-data' method='post'> \
            \ <input type='text' name='test-form.field1'/> \
            \ <input type='text' name='test-form.field2'/> \
            \ <input type='submit'/> \
            \ </form></body></html>"

    post "/" $ do
        (view, result) <- runForm "test-form" testForm
        case result of
            Just d -> ... Success! `d` contains constructed FormData.
            _ -> ... Failure! Use `view` variable to access error messages.