Implement motion editing
[cacert-boardvoting.git] / forms.go
index f3cd942..503e7e5 100644 (file)
--- a/forms.go
+++ b/forms.go
@@ -54,3 +54,45 @@ func (f *NewDecisionForm) Validate() (bool, *Decision) {
 
        return len(f.Errors) == 0, data
 }
+
+type EditDecisionForm struct {
+       Title    string
+       Content  string
+       VoteType string
+       Due      string
+       Decision *Decision
+       Errors   map[string]string
+}
+
+func (f *EditDecisionForm) Validate() (bool, *Decision) {
+       f.Errors = make(map[string]string)
+
+       data := f.Decision
+
+       data.Title = strings.TrimSpace(f.Title)
+       if len(data.Title) < 3 {
+               f.Errors["Title"] = "Please enter at least 3 characters."
+       }
+
+       data.Content = strings.TrimSpace(f.Content)
+       if len(strings.Fields(data.Content)) < 3 {
+               f.Errors["Content"] = "Please enter at least 3 words."
+       }
+
+       if voteType, err := strconv.ParseUint(f.VoteType, 10, 8); err != nil || (voteType != 0 && voteType != 1) {
+               f.Errors["VoteType"] = fmt.Sprint("Please choose a valid vote type", err)
+       } else {
+               data.VoteType = VoteType(uint8(voteType))
+       }
+
+       if dueDuration, ok := validDueDurations[f.Due]; !ok {
+               f.Errors["Due"] = "Please choose a valid due date"
+       } else {
+               year, month, day := time.Now().UTC().Add(dueDuration).Date()
+               data.Due = time.Date(year, month, day, 23, 59, 59, 0, time.UTC)
+       }
+
+       data.Modified = time.Now().UTC()
+
+       return len(f.Errors) == 0, data
+}