-
Notifications
You must be signed in to change notification settings - Fork 0
/
charfield.go
91 lines (83 loc) · 2.91 KB
/
charfield.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
package aform
// CharField is a field type that doesn't do semantic validation on the given
// value.
type CharField struct {
initialValue string
emptyValue string
*Field
}
// verify interface compliance
var _ fieldInterface = (*CharField)(nil)
// NewCharField creates a char field named name. The parameter initial is the
// initial value before data bounding. The parameter empty is the cleaned data
// value when there is no data bound to the field. If the parameter min
// (respectively max) is not 0, it validates that the input value is longer or
// equal than min (respectively shorter or equal than max). Otherwise, all
// inputs are valid. The default Widget is TextInput. To change it, use
// WithWidget or SetWidget.
func NewCharField(name, initial, empty string, min, max uint, opts ...FieldOption) (*CharField, error) {
cf := &CharField{
initial,
empty,
&Field{
name: name,
boundValues: []string{initial},
errors: []Error{},
fieldType: CharFieldType,
widget: TextInput,
autoID: defaultAutoID,
label: name,
labelSuffix: defaultLabelSuffix,
minLength: min,
maxLength: max,
locale: defaultLanguage,
},
}
cf.Field.validateFunc = charFieldValidationFunc(cf)
for _, opt := range opts {
if err := opt(cf.Field); err != nil {
return nil, err
}
}
return cf, nil
}
// DefaultCharField creates a char field with reasonable default values.
// initial and empty parameters are the empty string. min length is 0
// and max length is 256.
func DefaultCharField(name string, opts ...FieldOption) (*CharField, error) {
return NewCharField(name, "", "", 0, 256, opts...)
}
func (fld *CharField) field() *Field {
return fld.Field
}
// Clean returns the cleaned value. value is first sanitized and
// finally validated. Sanitization can be customized with
// Field.SetSanitizeFunc. Validation can be customized with
// Field.SetValidateFunc.
func (fld *CharField) Clean(value string) (string, []Error) {
fld.boundValues = []string{value}
sanitizedValue := fld.sanitize(value)
if fld.notRequired && len(sanitizedValue) == 0 {
return fld.EmptyValue(), nil
}
fld.errors = customizeErrors(fld.validateFunc(sanitizedValue, !fld.notRequired), fld.customErrors)
return sanitizedValue, fld.errors
}
// EmptyValue returns the CharField empty value. The empty value is the
// cleaned value returned by Clean when there is no data bound to the field.
// To set a custom empty value use NewCharField.
func (fld *CharField) EmptyValue() string {
return fld.emptyValue
}
func charFieldValidationFunc(fld *CharField) func(string, bool) []Error {
return func(value string, required bool) []Error {
var rules []string
if fld.minLength > 0 {
rules = append(rules, buildValidationMinRule(fld.minLength))
}
if fld.maxLength > 0 {
rules = append(rules, buildValidationMaxRule(fld.maxLength))
}
return validateValue(value, buildValidationRules(required, rules...))
}
}