-
Notifications
You must be signed in to change notification settings - Fork 1
/
feedback.go
125 lines (113 loc) · 2.7 KB
/
feedback.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
Package feedback is a standard contact form.
*/
package feedback
import (
"context"
"errors"
"regexp"
"strings"
"github.com/dkotik/htadaptor"
"github.com/nicksnyder/go-i18n/v2/i18n"
)
var reValidEmail = regexp.MustCompile(`^[^\@]+\@[^\@]+\.\w+$`)
type Sender func(context.Context, *Letter) error
type Letter struct {
Name string
Phone string
Email string
Message string
}
func (l *Letter) Validate(ctx context.Context) error {
locale, ok := htadaptor.LocalizerFromContext(ctx)
if !ok {
return errors.New("there is no localizer in context")
}
// separating localized validation, because HTMX handler may
// call it directly
return l.ValidateWithLocale(locale)
}
func newRequiredError(field string, l *i18n.Localizer) error {
msg, err := l.Localize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "Required",
Description: "Displayed when validation failed because of a missing input field.",
Other: "Please provide {{.Field}}.",
},
TemplateData: map[string]any{
"Field": strings.ToLower(field),
},
})
if err != nil {
return err
}
return errors.New(msg)
}
func (l *Letter) ValidateWithLocale(locale *i18n.Localizer) error {
if len(l.Name) < 4 {
field, err := l.nameLabel(locale)
if err != nil {
return err
}
return newRequiredError(field, locale)
}
if len(l.Email) < 4 {
field, err := l.emailLabel(locale)
if err != nil {
return err
}
return newRequiredError(field, locale)
}
if !reValidEmail.MatchString(l.Email) {
errorMessage, err := locale.Localize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "EmailFormatError",
Other: "Invalid electronic mail address.",
},
})
if err != nil {
return err
}
return errors.New(errorMessage)
}
if len(l.Message) < 4 {
field, err := l.messageLabel(locale)
if err != nil {
return err
}
return newRequiredError(field, locale)
}
return nil
}
func (l *Letter) nameLabel(locale *i18n.Localizer) (string, error) {
return locale.Localize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "Name",
Other: "Your Name",
},
})
}
func (l *Letter) phoneLabel(locale *i18n.Localizer) (string, error) {
return locale.Localize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "Phone",
Other: "Phone Number",
},
})
}
func (l *Letter) emailLabel(locale *i18n.Localizer) (string, error) {
return locale.Localize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "Email",
Other: "Email Address",
},
})
}
func (l *Letter) messageLabel(locale *i18n.Localizer) (string, error) {
return locale.Localize(&i18n.LocalizeConfig{
DefaultMessage: &i18n.Message{
ID: "Message",
Other: "Message",
},
})
}