This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_input_time.go
89 lines (76 loc) · 1.72 KB
/
validate_input_time.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package form
import (
"fmt"
"time"
)
func (va validateValue) timeInputTime(value time.Time) {
formatter := func(t time.Time) (str string) {
switch va._type {
case InputDatetime:
str = fmt.Sprint(t.Format(dateTimeFormat))
case InputDatetimeLocal:
str = fmt.Sprint(t.Format(dateTimeLocalFormat))
case InputTime:
str = fmt.Sprint(t.Format(timeFormat))
case InputDate:
str = fmt.Sprint(t.Format(dateFormat))
case InputMonth:
str = fmt.Sprint(t.Format(monthFormat))
case InputWeek:
year, week := t.ISOWeek()
str = fmt.Sprintf(weekFormat, year, week)
}
return
}
min := time.Time{}
max := time.Time{}
minErr := ""
maxErr := ""
va.fieldsFns.Call("range", map[string]interface{}{
"min": &min,
"max": &max,
"minErr": &minErr,
"maxErr": &maxErr,
})
if min.IsZero() && max.IsZero() {
goto check_mandatory
} else if min.IsZero() {
goto check_max
}
if value.Unix() < min.Unix() {
if minErr == "" {
minErr = va.form.T("ErrTimeMin", map[string]interface{}{
"Time": formatter(min),
})
*(va.err) = fmt.Errorf(minErr)
return
}
}
check_max:
if max.IsZero() {
goto check_mandatory
}
if value.Unix() > max.Unix() {
if maxErr == "" {
maxErr = va.form.T("ErrTimeMax", map[string]interface{}{
"Time": formatter(max),
})
*(va.err) = fmt.Errorf(maxErr)
return
}
}
check_mandatory:
manErr := ""
mandatory := false
va.fieldsFns.Call("mandatory", map[string]interface{}{
"mandatory": &mandatory,
"err": &manErr,
})
if mandatory && value.IsZero() {
if manErr == "" {
manErr = va.form.T("ErrMandatory")
}
*(va.err) = fmt.Errorf(manErr)
return
}
}