I am implementing a Contact Me form that will send an email when it is sent. I need this form to fix custom HTML, so I ended up using monadic forms. The problem is that I do not know how to use the monadic form.
code below. I missed the part that sends emails for brevity. the problem is that my form is never validated correctly. the result of the form is never FormSuccess in my postContactR function.
It seems that I am not initializing the form correctly when I call runFormPost inside postContactR . I always pass Nothing instead of the actual ContactData in contactForm , and I don't know how to build my ContactData from the request. Do I understand the problem correctly? I am trying to work with poorly documented functions. :)
any help?
EDIT: It looks strange that validation errors appear on the form if I submit an invalid form, so the request data is read at some point. what does not work is that in the absence of errors, I do not redirect 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
source share