This repository has been archived on 2021-09-01. You can view files and clone it, but cannot push or open issues or pull requests.
goddit/web/forms.go
2021-08-29 18:22:08 +02:00

68 lines
1.1 KiB
Go

package web
import "encoding/gob"
func init() {
gob.Register(CreatePostForm{})
gob.Register(CreateThreadForm{})
gob.Register(CreateCommentForm{})
gob.Register(FormErrors{})
}
type FormErrors map[string]string
type CreatePostForm struct {
Title string
Content string
Errors FormErrors
}
func (f *CreatePostForm) Validate() bool {
f.Errors = FormErrors{}
if f.Title == "" {
f.Errors["Title"] = "Please enter a title!"
}
if f.Content == "" {
f.Errors["Content"] = "Please enter a text!"
}
return len(f.Errors) == 0
}
type CreateThreadForm struct {
Title string
Description string
Errors FormErrors
}
func (f *CreateThreadForm) Validate() bool {
f.Errors = FormErrors{}
if f.Title == "" {
f.Errors["Title"] = "Please enter a title!"
}
if f.Description == "" {
f.Errors["Description"] = "Please enter a description!"
}
return len(f.Errors) == 0
}
type CreateCommentForm struct {
Content string
Errors FormErrors
}
func (f *CreateCommentForm) Validate() bool {
f.Errors = FormErrors{}
if f.Content == "" {
f.Errors["Content"] = "Please enter a comment!"
}
return len(f.Errors) == 0
}