-
Notifications
You must be signed in to change notification settings - Fork 0
/
contactinfo.go
68 lines (61 loc) · 2.45 KB
/
contactinfo.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
package contact_info
import "fmt"
//=================================================================
// ContactInfo
type ContactInfo struct {
Binding string `mapstructure:"binding"`
Platform string `mapstructure:"platform"`
Firstname string `mapstructure:"firstname"`
Lastname string `mapstructure:"lastname"`
Email string `mapstructure:"email"`
Phone string `mapstructure:"phone"`
Company string `mapstructure:"company"`
Address string `mapstructure:"address"`
Zip string `mapstructure:"zip"`
City string `mapstructure:"city"`
Country string `mapstructure:"country"`
State string `mapstructure:"state"`
}
func validateContactInfoField(fieldName, fieldValue string, minLength, maxLength int) error {
if len(fieldValue) < minLength {
return fmt.Errorf("%s field cannot be less than %d", fieldName, minLength)
} else if len(fieldValue) > maxLength {
return fmt.Errorf("%s field cannot exceed %d characters (%d)", fieldName, maxLength, len(fieldValue))
}
return nil
}
func (contactInfo ContactInfo) Validate() error {
if err := validateContactInfoField("contactinfo.firstname", contactInfo.Firstname, 2, 40); err != nil {
return err
} else if err := validateContactInfoField("contactinfo.lastname", contactInfo.Lastname, 2, 40); err != nil {
return err
} else if err := validateContactInfoField("contactinfo.email", contactInfo.Email, 2, 241); err != nil {
return err
} else if err := validateContactInfoField("contactinfo.phone", contactInfo.Phone, 2, 25); err != nil {
return err
} else if err := validateContactInfoField("contactinfo.company", contactInfo.Company, 2, 40); err != nil {
return err
} else if err := validateContactInfoField("contactinfo.address", contactInfo.Address, 2, 60); err != nil {
return err
} else if err := validateContactInfoField("contactinfo.zip", contactInfo.Zip, 2, 10); err != nil {
return err
} else if err := validateContactInfoField("contactinfo.city", contactInfo.City, 2, 40); err != nil {
return err
}
return nil
}
func (contactInfo ContactInfo) GetFormData() map[string]string {
return map[string]string{
"terms": "true",
"firstname": contactInfo.Firstname,
"lastname": contactInfo.Lastname,
"email": contactInfo.Email,
"phone": contactInfo.Phone,
"company": contactInfo.Company,
"address": contactInfo.Address,
"zip": contactInfo.Zip,
"city": contactInfo.City,
"country": contactInfo.Country,
"state": contactInfo.State,
}
}