I guess what I was looking for is the validate
function and a little help from maybeRead
. This appears to work:
dateTimeForm :: Monad m => Maybe LocalTime -> Form Text m LocalTime
dateTimeForm t = LocalTime
<$> "date" .: stringRead "Must be a valid date" (localDay <$> t)
<*> "time" .: validate validTime (string (show . localTimeOfDay <$> t))
where
-- the time input element only submits hours and minutes ("18:00"), which is 5 characters long
validTime x = case maybeRead (if length x == 5 then x <> ":00" else x) of
Just x' -> Success x'
_ -> Error "Must be a valid time"