-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforms.go
More file actions
596 lines (502 loc) · 14.1 KB
/
forms.go
File metadata and controls
596 lines (502 loc) · 14.1 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package go_forms
import (
"encoding/json"
"net"
"regexp"
"strconv"
)
type Field interface {
GetId() string
ShouldDisplay() bool
IsValid() bool
GetValue() string
SetValue(value string)
GetError() error
}
type Validator interface {
Validate(field any) bool
}
type DisplayCondition interface {
DisplayCondition(field any) bool
}
// Defining the Base Field Type
type FieldBaseType struct {
Id string
DisplayConditions []DisplayCondition
Validators []Validator
Value string
form *Form
error error
}
func (f *FieldBaseType) GetId() string {
return f.Id
}
func (f *FieldBaseType) ShouldDisplay() bool {
for _, displayCondition := range f.DisplayConditions {
if !displayCondition.DisplayCondition(f) {
return false
}
}
return true
}
func (f *FieldBaseType) IsValid() bool {
if !f.ShouldDisplay() {
return true
}
for _, validator := range f.Validators {
if !validator.Validate(f) {
return false
}
}
f.error = nil
return true
}
func (f *FieldBaseType) GetValue() string {
return f.Value
}
func (f *FieldBaseType) SetValue(value string) {
f.Value = value
if f.form != nil {
f.form.onChange()
}
}
func (f *FieldBaseType) GetError() error {
return f.error
}
type CustomValidator struct {
Validator func(field any) (bool, error)
}
func (v *CustomValidator) Validate(field any) bool {
valid, err := v.Validator(field)
if err != nil {
field.(*FieldBaseType).error = err
}
return valid
}
type AllFieldsValid struct{}
func (v *AllFieldsValid) Validate(field any) bool {
fields := field.(*FieldBaseType).form.GetAllFields()
for _, f := range fields {
if !f.IsValid() {
field.(*FieldBaseType).error = &CustomError{Message: "Not all fields are valid (invalid field: " + f.GetId() + ")"}
return false
}
}
return true
}
type IsValidValidator struct {
FieldIds []string
}
func (v *IsValidValidator) Validate(field any) bool {
fields := field.(*FieldBaseType).form.GetAllFields()
for _, f := range fields {
for _, id := range v.FieldIds {
if f.GetId() == id && !f.IsValid() {
field.(*FieldBaseType).error = &CustomError{Message: "Not all fields that should be valid are valid (invalid field: " + f.GetId() + ")"}
return false
}
}
}
return true
}
type AlwaysDisplay struct{}
func (d *AlwaysDisplay) DisplayCondition(_ any) bool {
return true
}
type CustomDisplayCondition struct {
Condition func(field any) bool
}
func (d *CustomDisplayCondition) DisplayCondition(field any) bool {
return d.Condition(field)
}
type IsValidDisplayCondition struct {
FieldIds []string
}
func (d *IsValidDisplayCondition) DisplayCondition(field any) bool {
fields := field.(*FieldBaseType).form.GetAllFields()
for _, f := range fields {
for _, id := range d.FieldIds {
if f.GetId() == id && !f.IsValid() {
return false
}
}
}
return true
}
type IsInvalidDisplayCondition struct {
FieldIds []string
}
func (d *IsInvalidDisplayCondition) DisplayCondition(field any) bool {
fields := field.(*FieldBaseType).form.GetAllFields()
for _, f := range fields {
for _, id := range d.FieldIds {
if f.GetId() == id && f.IsValid() {
return false
}
}
}
return true
}
type AllFieldsValidDisplayCondition struct{}
func (d *AllFieldsValidDisplayCondition) DisplayCondition(field any) bool {
fields := field.(*FieldBaseType).form.GetAllFields()
for _, f := range fields {
if !f.IsValid() {
return false
}
}
return true
}
type HasValueDisplayCondition struct {
FieldId string
Value string
}
func (d *HasValueDisplayCondition) DisplayCondition(field any) bool {
fields := field.(*FieldBaseType).form.GetAllFields()
for _, f := range fields {
if f.GetId() == d.FieldId && f.GetValue() == d.Value {
return true
}
}
return false
}
type DisplayAfter struct {
FieldId string
}
func (d *DisplayAfter) DisplayCondition(field any) bool {
fields := field.(*FieldBaseType).form.GetAllFields()
for _, f := range fields {
if f.GetId() == d.FieldId {
return f.IsValid() && f.ShouldDisplay()
}
}
return false
}
type OrDisplayCondition struct {
Conditions []DisplayCondition
}
func (d *OrDisplayCondition) DisplayCondition(field any) bool {
for _, condition := range d.Conditions {
if condition.DisplayCondition(field) {
return true
}
}
return false
}
type AndDisplayCondition struct {
Conditions []DisplayCondition
}
func (d *AndDisplayCondition) DisplayCondition(field any) bool {
for _, condition := range d.Conditions {
if !condition.DisplayCondition(field) {
return false
}
}
return true
}
// Defining the Field Types based on the Base Field Type
type Message struct {
*FieldBaseType
}
// Defining the Text Field Type based on the Base Field Type
type TextField struct {
*FieldBaseType
Placeholder string
Prompt string
}
type NotEmptyValidator struct{}
func (v *NotEmptyValidator) Validate(field any) bool {
value := field.(*FieldBaseType).Value
valid := value != ""
if !valid {
field.(*FieldBaseType).error = &CustomError{Message: "Field cannot be empty"}
}
return valid
}
type MaxLengthValidator struct {
MaxLength int
}
func (v *MaxLengthValidator) Validate(field any) bool {
value := field.(*FieldBaseType).Value
valid := len(value) <= v.MaxLength
if !valid {
field.(*FieldBaseType).error = &CustomError{Message: "Field is too long (length: " + strconv.Itoa(len(value)) + ", max length: " + strconv.Itoa(v.MaxLength) + ")"}
}
return valid
}
type MinLengthValidator struct {
MinLength int
}
func (v *MinLengthValidator) Validate(field any) bool {
value := field.(*FieldBaseType).Value
valid := len(value) >= v.MinLength
if !valid {
field.(*FieldBaseType).error = &CustomError{Message: "Field is too short (length: " + strconv.Itoa(len(value)) + ", min length: " + strconv.Itoa(v.MinLength) + ")"}
}
return valid
}
type IpValidator struct{}
func (v *IpValidator) Validate(field any) bool {
value := field.(*FieldBaseType).Value
valid := net.ParseIP(value) != nil
if !valid {
field.(*FieldBaseType).error = &CustomError{Message: "Field is not a valid IP address"}
}
return valid
}
type RegexValidator struct {
RegexPattern string
}
func (v *RegexValidator) Validate(field any) bool {
value := field.(*FieldBaseType).Value
valid := true
if value != "" {
valid = regexp.MustCompile(v.RegexPattern).MatchString(value)
}
if !valid {
field.(*FieldBaseType).error = &CustomError{Message: "Field does not match the required pattern (" + v.RegexPattern + ")"}
}
return valid
}
type UrlValidator struct{}
func (v *UrlValidator) Validate(field any) bool {
value := field.(*FieldBaseType).Value
valid := regexp.MustCompile(`^https?://.`).MatchString(value)
if !valid {
field.(*FieldBaseType).error = &CustomError{Message: "Field is not a valid URL"}
}
return valid
}
func (t *TextField) GetPlaceholder() string {
return t.Placeholder
}
func (t *TextField) GetPrompt() string {
return t.Prompt
}
// Defining the Number Field Type based on the Base Field Type
type NumberField struct {
*TextField
}
type MinValidator struct {
Min int
}
func (v *MinValidator) Validate(field any) bool {
value := field.(*FieldBaseType).Value
valueAsInt, err := strconv.Atoi(value)
if err != nil {
field.(*FieldBaseType).error = &CustomError{Message: "Field value is not a integer"}
return false
}
valid := v.Min <= valueAsInt
if !valid {
field.(*FieldBaseType).error = &CustomError{Message: "Field value is too small (value: " + value + ", min value: " + strconv.Itoa(v.Min) + ")"}
}
return valid
}
type MaxValidator struct {
Max int
}
func (v *MaxValidator) Validate(field any) bool {
value := field.(*FieldBaseType).Value
valueAsInt, err := strconv.Atoi(value)
if err != nil {
field.(*FieldBaseType).error = &CustomError{Message: "Field value is not a integer"}
return false
}
valid := valueAsInt <= v.Max
if !valid {
field.(*FieldBaseType).error = &CustomError{Message: "Field value is too big (value: " + value + ", max value: " + strconv.Itoa(v.Max) + ")"}
}
return valueAsInt <= v.Max
}
type IsIntegerValidator struct{}
func (v *IsIntegerValidator) Validate(field any) bool {
value := field.(*FieldBaseType).Value
_, err := strconv.Atoi(value)
if err != nil {
field.(*FieldBaseType).error = &CustomError{Message: "Field value is not a integer"}
}
return err == nil
}
// Defining the Multiple Choice Field Type based on the Text Field Type
type MultipleChoiceField struct {
*TextField
Options map[string]Option
}
type Option struct {
Label string
Description string
}
type ChoiceValidator struct{}
func (v *ChoiceValidator) Validate(field any) bool {
multipleChoiceField, ok := field.(*MultipleChoiceField)
if !ok {
multipleChoiceField.error = &CustomError{Message: "Field is not a multiple choice field but ChoiceValidator was used"}
return false
}
_, ok = multipleChoiceField.Options[multipleChoiceField.Value]
if !ok {
multipleChoiceField.error = &CustomError{Message: "Field value is not a valid option"}
}
return ok
}
func (m *MultipleChoiceField) GetOptions() map[string]Option {
return m.Options
}
func (m *MultipleChoiceField) IsValid() bool {
if !m.ShouldDisplay() {
return true
}
for _, validator := range m.Validators {
if !validator.Validate(m) {
return false
}
}
m.error = nil
return true
}
// Defining the Field Group Type based on the Base Field Type
type FieldGroup struct {
*FieldBaseType
Fields []Field
heading string
}
func (f *FieldGroup) GetFieldsToDisplay() []Field {
var fieldsToDisplay []Field
for _, field := range f.Fields {
if field.ShouldDisplay() {
fieldsToDisplay = append(fieldsToDisplay, field)
}
}
return fieldsToDisplay
}
func (f *FieldGroup) GetFieldById(id string) Field {
for _, field := range f.Fields {
if field.GetId() == id {
return field
}
}
return nil
}
func (f *FieldGroup) GetValue() string {
fieldValues := make(map[string]string)
for _, field := range f.Fields {
fieldValues[field.GetId()] = field.GetValue()
}
jsonFieldValues, _ := json.Marshal(fieldValues)
return string(jsonFieldValues)
}
func (f *FieldGroup) GetHeading() string {
return f.heading
}
func (f *FieldGroup) SetValue(value string) {
var fieldValues map[string]string
err := json.Unmarshal([]byte(value), &fieldValues)
if err != nil {
return
}
for _, field := range f.Fields {
field.SetValue(fieldValues[field.GetId()])
}
}
func (f *FieldGroup) SetHeading(heading string) {
f.heading = heading
}
// Defining the Form Type
type Form struct {
Fields []Field
onChange func()
}
func (f *Form) GetAllFields() []Field {
fields := make([]Field, 0)
for _, field := range f.Fields {
fields = append(fields, field)
if group, ok := field.(*FieldGroup); ok {
fields = append(fields, group.Fields...)
}
}
return fields
}
func (f *Form) IsValid() bool {
for _, field := range f.Fields {
if !field.IsValid() {
return false
}
}
return true
}
func (f *Form) GetFieldById(id string) Field {
for _, field := range f.Fields {
if field.GetId() == id {
return field
}
}
return nil
}
func (f *Form) GetFieldsToDisplay() []Field {
var fieldsToDisplay []Field
for _, field := range f.Fields {
if field.ShouldDisplay() {
fieldsToDisplay = append(fieldsToDisplay, field)
}
}
return fieldsToDisplay
}
func (f *Form) GetFieldValues() map[string]string {
fieldValues := make(map[string]string)
for _, field := range f.Fields {
fieldValues[field.GetId()] = field.GetValue()
}
return fieldValues
}
func (f *Form) SetOnChangeCallback(onChange func()) {
f.onChange = onChange
}
func (f *Form) GetError() error {
for _, field := range f.Fields {
if !field.IsValid() {
return &CustomError{Message: field.GetId() + " is not valid (" + field.GetError().Error() + ")"}
}
}
return nil
}
// Defining the form builder functions
// NewForm creates a new form with the given fields
func NewForm(fields ...Field) *Form {
form := &Form{Fields: fields, onChange: func() {}}
for _, field := range fields {
switch v := field.(type) {
case *FieldBaseType:
v.form = form
case *TextField:
v.FieldBaseType.form = form
case *NumberField:
v.TextField.FieldBaseType.form = form
case *MultipleChoiceField:
v.TextField.FieldBaseType.form = form
case *FieldGroup:
v.FieldBaseType.form = form
}
}
return form
}
// NewFieldGroup creates a new field group with the given fields
func NewFieldGroup(id string, displayConditions []DisplayCondition, validators []Validator, heading string, fields ...Field) *FieldGroup {
return &FieldGroup{FieldBaseType: &FieldBaseType{Id: id, DisplayConditions: displayConditions, Validators: validators}, Fields: fields, heading: heading}
}
// NewTextField creates a new text field with the given parameters
func NewTextField(id string, displayConditions []DisplayCondition, validators []Validator, placeholder string, prompt string, defaultValue string) *TextField {
return &TextField{FieldBaseType: &FieldBaseType{Id: id, DisplayConditions: displayConditions, Validators: validators, Value: defaultValue}, Placeholder: placeholder, Prompt: prompt}
}
// NewNumberField creates a new number field with the given parameters
func NewNumberField(id string, displayConditions []DisplayCondition, validators []Validator, placeholder string, prompt string, defaultValue int) *NumberField {
return &NumberField{TextField: &TextField{FieldBaseType: &FieldBaseType{Id: id, DisplayConditions: displayConditions, Validators: validators, Value: strconv.Itoa(defaultValue)}, Placeholder: placeholder, Prompt: prompt}}
}
// NewMultipleChoiceField creates a new multiple choice field with the given parameters
func NewMultipleChoiceField(id string, displayConditions []DisplayCondition, validators []Validator, placeholder string, prompt string, options map[string]Option, defaultValue string) *MultipleChoiceField {
return &MultipleChoiceField{TextField: &TextField{FieldBaseType: &FieldBaseType{Id: id, DisplayConditions: displayConditions, Validators: validators, Value: defaultValue}, Placeholder: placeholder, Prompt: prompt}, Options: options}
}
// NewMessage creates a new message with the given parameters
func NewMessage(id string, displayConditions []DisplayCondition, message string) *Message {
return &Message{FieldBaseType: &FieldBaseType{Id: id, DisplayConditions: displayConditions, Validators: []Validator{}, Value: message}}
}