blog/internal/forms/contact.go

49 lines
895 B
Go
Raw Permalink Normal View History

2017-12-23 02:34:58 +00:00
package forms
import (
"errors"
"net/http"
)
// Contact form for the site admin.
type Contact struct {
Name string
Email string
Subject string
Message string
Trap1 string // 'contact'
Trap2 string // 'website'
}
// ParseForm parses the form.
func (c *Contact) ParseForm(r *http.Request) {
c.Name = r.FormValue("name")
c.Email = r.FormValue("email")
c.Subject = r.FormValue("subject")
c.Message = r.FormValue("message")
c.Trap1 = r.FormValue("contact")
c.Trap2 = r.FormValue("website")
// Default values.
if c.Name == "" {
c.Name = "Anonymous"
}
if c.Subject == "" {
c.Subject = "No Subject"
}
}
// Validate the form.
func (c Contact) Validate() error {
if len(c.Message) == 0 {
return errors.New("message is required")
}
2018-05-01 18:26:50 +00:00
// Spam bot traps.
if c.Trap1 != "" || c.Trap2 != "http://" {
return errors.New("message can't be delivered")
}
2017-12-23 02:34:58 +00:00
return nil
}