-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls-time.go
72 lines (62 loc) · 1.47 KB
/
controls-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
package forms
import (
"fmt"
"time"
)
type InputTime struct {
RenderableImpl[InputTime]
Template
TemplateStyle
Field
TagOpts
*Binding[time.Time]
InputWellOpts
IncludeTime bool
Min time.Time
Max time.Time
Location *time.Location
}
const (
DateTimeLocalInputTimeFormat = "2006-01-02T15:04"
)
func (InputTime) DefaultTemplate() string { return "control-datetime-date" }
func (c *InputTime) InputType() string {
if c.IncludeTime {
return "datetime-local"
} else {
return "date"
}
}
func (c *InputTime) ValueFormat() string {
if c.IncludeTime {
return DateTimeLocalInputTimeFormat
} else {
return time.DateOnly
}
}
func (c *InputTime) PresentationFormat() string {
if c.IncludeTime {
return "2006-01-02 15:04"
} else {
return time.DateOnly
}
}
func (c *InputTime) Finalize(state *State) {
c.Validate(func(value time.Time) (time.Time, error) {
if !c.Min.IsZero() && value.Before(c.Min) {
return value, fmt.Errorf("must be on or after %v", c.Min.Format(c.PresentationFormat()))
}
if !c.Max.IsZero() && value.After(c.Max) {
return value, fmt.Errorf("must be before %v", c.Max.Format(c.PresentationFormat()))
}
return value, nil
})
}
func (c *InputTime) Process(*FormData) {
c.Binding.SetString(c.RawFormValue, func(s string) (time.Time, error) {
return time.ParseInLocation(c.ValueFormat(), s, c.Location)
})
}
func (c *InputTime) FormattedValue() string {
return c.Binding.Value().In(c.Location).Format(c.ValueFormat())
}