-
Notifications
You must be signed in to change notification settings - Fork 6
/
form.go
56 lines (49 loc) · 1.48 KB
/
form.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (c) 2016, Janoš Guljaš <[email protected]>
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package web
// FormErrors represent structure errors returned by API server to
// request based on HTML form data.
type FormErrors struct {
Errors []string `json:"errors,omitempty"`
FieldErrors map[string][]string `json:"field_errors,omitempty"`
}
// AddError appends an error to a list of general errors.
func (f *FormErrors) AddError(e string) {
f.Errors = append(f.Errors, e)
}
// AddFieldError appends an error to a list of field specific errors.
func (f *FormErrors) AddFieldError(field, e string) {
if f.FieldErrors == nil {
f.FieldErrors = map[string][]string{}
}
if _, ok := f.FieldErrors[field]; !ok {
f.FieldErrors[field] = []string{}
}
f.FieldErrors[field] = append(f.FieldErrors[field], e)
}
// HasErrors returns weather FormErrors instance contains at leas one error.
func (f FormErrors) HasErrors() bool {
if len(f.Errors) > 0 {
return true
}
for _, v := range f.FieldErrors {
if len(v) > 0 {
return true
}
}
return false
}
// NewError initializes FormErrors with one general error.
func NewError(e string) FormErrors {
errors := FormErrors{}
errors.AddError(e)
return errors
}
// NewFieldError initializes FormErrors with one field error.
func NewFieldError(field, e string) FormErrors {
errors := FormErrors{}
errors.AddFieldError(field, e)
return errors
}