-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.go
More file actions
64 lines (46 loc) · 1008 Bytes
/
Copy pathforms.go
File metadata and controls
64 lines (46 loc) · 1008 Bytes
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
57
58
59
60
61
62
63
64
package forms
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/asaskevich/govalidator"
)
type Form struct {
url.Values
Errors errors
}
func NewForm(data url.Values) *Form {
return &Form{
data,
errors(map[string][]string{}),
}
}
func (f *Form) HasRequired(IdTags ...string) {
for _, Idtag := range IdTags {
value := f.Get(Idtag)
if strings.TrimSpace(value) == "" {
f.Errors.AddError(Idtag, "This field can't be blank")
}
}
}
func (f *Form) HasValue(IdTag string, r *http.Request) bool {
x := r.Form.Get(IdTag)
return x != ""
}
func (f *Form) MinLength(IdTag string, lenght int, r *http.Request) bool {
x := r.Form.Get(IdTag)
if len(x) < lenght {
f.Errors.AddError(IdTag, fmt.Sprintf("must be %d charcaters long or more ", lenght))
return false
}
return true
}
func (f *Form) IsEmail(IdTag string) {
if !govalidator.IsEmail(f.Get(IdTag)) {
f.Errors.AddError(IdTag, "Invalid Email")
}
}
func (f *Form) Valid() bool {
return len(f.Errors) == 0
}