Take the 2-minute tour ×

I am implementing a "contact me" form that will send an email when it is submitted. I needed this form to emit custom HTML, so I ended up using monadic forms. The problem is that I do not know how to use a monadic form.

the code is below. I have omitted the part that sends e-mail for brevity. the problem is that my form never validates correctly. the form result is never FormSuccess in my postContactR function.

It seems that I do not initialize the form correctly when I call runFormPost inside postContactR. I always pass Nothing instead of the actual ContactData to contactForm and I do not know how to construct my ContactData from the request. Is my understanding of the problem correct? I am trying to work with poorly documented features. :)

any help?

EDIT: what looks strange is that validation errors do show up in the form if I submit an invalid form, so the request data does get read at some point. what does not work is that when there are no errors I do not get redirected to RootR

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
module Handler.Contact where
import Control.Applicative ((<$>), (<*>)) import Data.Text (Text) import Foundation import Network.Mail.Mime
data ContactData = ContactData { contactName :: Text , contactEmail :: Text , contactMessage :: Textarea } deriving Show
contactForm d = \html -> do (r1, v1) <- mreq textField "Your name:" (contactName <$> d) (r2, v2) <- mreq emailField "Your e-mail:" (contactEmail <$> d) (r3, v3) <- mreq textareaField "Message:" (contactMessage <$> d) let views = [v1, v2, v3] return (ContactData <$> r1 <*> r2 <*> r3, $(widgetFile "contact-form"))
getContactR :: Handler RepHtml getContactR = do ((_, form), _) <- runFormPost (contactForm Nothing) defaultLayout $ do setTitle "contact" addWidget $(widgetFile "contact")
postContactR :: Handler RepHtml postContactR = do ((r, form), _) <- runFormPost (contactForm Nothing) case r of FormSuccess d -> do sendEmail d setMessage "Message sent" redirect RedirectTemporary RootR _ -> getContactR

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Are you including the html value in contact-form.hamlet? It's a nonce value. You'd get better debug information if you printed the value of r (in postContactR).

I have on my writing TODO list to add a monadic form example, it should be up soon.

share|improve this answer
thanks. I did not have it. that was the reason. –  akonsu Sep 22 '11 at 2:49
add comment

Your Answer


