Commits

Luke Plant committed 88b9e89

Changed validation errors to be a Map instead of list.

  • Participants
  • Parent commits aa7bd76

Comments (0)

Files changed (3)

src/Blog/Forms.hs

 import Blog.Formats (Format(..), getFormatter)
 
 import Data.Maybe (fromJust)
+import qualified Data.Map as Map
 import System.Posix.Time (epochTime)
 import System.Posix.Types
 
 
 
 -- | extract the posted data from a POST request and build
--- a Comment from it, returning a Comment and a list of validaion errors
+-- a Comment from it, returning a Comment and a list of validation errors
 validateComment postedData blogpost =
     do
     -- TODO - protect name -- some names are reversed for logged in users.
     -- TODO - posts that are closed for comments
-    -- TODO - addCommentToPost utility
     -- TODO - nicer mechanism for validation
+    -- TODO - validate lengths of fields
+    -- TODO - CSRF protection
       ts <- epochTime
       let text = postedData "message" `captureOrDefault` ""
       let name = postedData "name" `captureOrDefault` ""
       let email = postedData "email" `captureOrDefault` ""
       let errors = (if null text
-                   then ["'Message' is a required field."]
+                   then [("message", "'Message' is a required field.")]
                    else [])
       let format = Plaintext
 
                     , text_raw = text
                     , text_formatted = getFormatter format $ text
                     , format = format
-                    }, errors)
+                    }, Map.fromList errors)
 

src/Blog/Templates.hs

 import Ella.Forms.Widgets (makeLabel)
 import Ella.Forms.Base
 import Data.List (intersperse)
+import qualified Data.Map as Map
 import Text.XHtml
 import qualified Blog.Post as P
 import qualified Blog.Category as C
 postPage :: P.Post        -- ^ The Post to display
          -> CommentStage  -- ^ What stage comment submission is at
          -> Cm.Comment    -- ^ Data for the comment form
-         -> [String]      -- ^ Validation errors for comment
+         -> Map.Map String String -- ^ Validation errors for comment
          -> [C.Category]  -- ^ Categories the post is in
          -> [Cm.Comment]  -- ^ Comments belonging to the poast
          -> [P.Post]      -- ^ Related posts
 
        CommentInvalid ->
            (thediv ! [theclass "validationerror"]
-            << unordList errors)
+            << unordList (Map.elems errors))
 
        _ -> noHtml
     )

src/Blog/Views.hs

 {-# OPTIONS_GHC -fglasgow-exts -XOverloadedStrings #-}
 module Blog.Views where
 
+import qualified Data.Map as Map
 import Ella.Framework (default404, View)
 import Ella.Param (captureOrDefault)
 import Ella.Request
         case requestMethod req of
           "POST" -> do
             (commentData, commentErrors) <- validateComment (getPOST req) post
-            if null commentErrors
+            if Map.null commentErrors
                then if isJust (getPOST req "submit")
                     then
                         do
                           addComment cn commentData
-                          return (CommentAccepted, emptyComment, [])
+                          return (CommentAccepted, emptyComment, Map.empty)
                           -- Just assume 'preview' if not 'submit'
                     else return (CommentPreview, commentData, commentErrors)
                else
                    return (CommentInvalid, commentData, commentErrors)
 
-          _ -> return (NoComment, emptyComment, [])
+          _ -> return (NoComment, emptyComment, Map.empty)
 
 
 -- | View that shows a post as a static information page -- no comments etc.