Reputation: 29538
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
Upvotes: 4
Views: 309
Reputation: 31305
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.
Upvotes: 2