-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathforms.go
940 lines (862 loc) · 24.1 KB
/
forms.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
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
/*
Copyright 2016-present Wenhui Shen <www.webx.top>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package forms This package provides form creation and rendering functionalities, as well as FieldSet definition.
// Two kind of forms can be created: base forms and Bootstrap3 compatible forms; even though the latters are automatically provided
// the required classes to make them render correctly in a Bootstrap environment, every form can be given custom parameters such as
// classes, id, generic parameters (in key-value form) and stylesheet options.
package forms
import (
"bytes"
"fmt"
"html/template"
"net/url"
"reflect"
"strconv"
"strings"
"github.com/coscms/forms/common"
"github.com/coscms/forms/config"
"github.com/coscms/forms/fields"
"github.com/webx-top/validation"
)
// Form methods: POST or GET.
const (
POST = "POST"
GET = "GET"
)
func NewWithConfig(c *config.Config, args ...interface{}) *Form {
form := New()
form.Init(c, args...)
return form
}
func NewWithConfigFile(m interface{}, configJSONFile string) *Form {
config, err := UnmarshalFile(configJSONFile)
if err != nil {
panic(err)
}
return NewWithModelConfig(m, config)
}
func New() *Form {
return &Form{
FieldList: []config.FormElement{},
fieldMap: make(map[string]int),
containerMap: make(map[string]string),
Theme: common.BASE,
Class: common.HTMLAttrValues{},
ID: "",
Params: map[string]string{},
CSS: map[string]string{},
Method: POST,
AppendData: map[string]interface{}{},
labelFn: func(s string) string { return s },
validTagFn: Html5Validate,
beforeRender: []func(){},
omitOrMustFieldsValue: map[string]bool{},
}
}
func NewFromModel(m interface{}, c *config.Config) *Form {
form := NewWithConfig(c, m)
form.SetModel(m)
form.ParseModel(m)
return form
}
// Form structure.
type Form struct {
AppendData map[string]interface{} `json:"appendData,omitempty" xml:"appendData,omitempty"`
Model interface{} `json:"model" xml:"model"`
FieldList []config.FormElement `json:"fieldList" xml:"fieldList"`
Theme string `json:"theme" xml:"theme"`
Class common.HTMLAttrValues `json:"class" xml:"class"`
ID string `json:"id" xml:"id"`
Params map[string]string `json:"params" xml:"params"`
CSS map[string]string `json:"css" xml:"css"`
Method string `json:"method" xml:"method"`
Action template.HTML `json:"action" xml:"action"`
omitOrMustFieldsValue map[string]bool //true:omit; false:must
omitAllFieldsValue bool
fieldMap map[string]int
containerMap map[string]string
ignoreValid []string
valid *validation.Validation
labelFn func(string) string
validTagFn func(string, fields.FieldInterface)
config *config.Config
beforeRender []func()
debug bool
data map[string]interface{}
structFieldConverter func(string) string
}
func (f *Form) Reset() *Form {
f.AppendData = map[string]interface{}{}
f.Model = nil
f.FieldList = []config.FormElement{}
f.Theme = ``
f.Class = common.HTMLAttrValues{}
f.ID = ``
f.Params = map[string]string{}
f.CSS = map[string]string{}
f.Method = ``
f.Action = template.HTML(``)
f.omitOrMustFieldsValue = map[string]bool{}
f.omitAllFieldsValue = false
f.fieldMap = map[string]int{}
f.containerMap = map[string]string{}
f.ignoreValid = []string{}
f.valid = nil
f.labelFn = nil
f.validTagFn = nil
f.config = nil
f.beforeRender = []func(){}
f.debug = false
f.data = map[string]interface{}{}
f.structFieldConverter = nil
return f
}
func (f *Form) Debug(args ...bool) *Form {
if len(args) > 0 {
f.debug = args[0]
} else {
f.debug = true
}
return f
}
func (f *Form) SetStructFieldConverter(convert func(string) string) *Form {
f.structFieldConverter = convert
return f
}
func (f *Form) Config() *config.Config {
return f.config
}
func (f *Form) IsDebug() bool {
return f.debug
}
// Must 使用结构体实例中某些字段的值(和OmitAll配合起来使用)
func (f *Form) Must(fields ...string) *Form {
for _, field := range fields {
f.omitOrMustFieldsValue[field] = true
}
return f
}
func (f *Form) ResetOmitOrMust() *Form {
f.omitOrMustFieldsValue = map[string]bool{}
return f
}
// Omit 忽略结构体实例中某些字段的值
func (f *Form) Omit(fields ...string) *Form {
for _, field := range fields {
f.omitOrMustFieldsValue[field] = false
}
return f
}
// OmitAll 忽略结构体实例中所有字段的值
func (f *Form) OmitAll(on ...bool) *Form {
if len(on) > 0 {
f.omitAllFieldsValue = on[0]
} else {
f.omitAllFieldsValue = true
}
return f
}
// IsOmit 是否忽略结构体实例中指定字段的值
func (f *Form) IsOmit(fieldName string) (omitFieldValue bool) {
if f.omitAllFieldsValue {
//默认为忽略,设置为不忽略时才不忽略
omit, ok := f.omitOrMustFieldsValue[fieldName]
if !ok || omit {
omitFieldValue = true
}
} else {
//设置为忽略时,才忽略
omit, ok := f.omitOrMustFieldsValue[fieldName]
if ok && omit {
omitFieldValue = true
}
}
return
}
func (f *Form) SetLabelFunc(fn func(string) string) *Form {
f.labelFn = fn
return f
}
func (f *Form) SetValidTagFunc(fn func(string, fields.FieldInterface)) *Form {
f.validTagFn = fn
return f
}
func (f *Form) LabelFunc() func(string) string {
return f.labelFn
}
func (f *Form) ValidTagFunc() func(string, fields.FieldInterface) {
return f.validTagFn
}
func (f *Form) Init(c *config.Config, model ...interface{}) *Form {
if c == nil {
c = &config.Config{}
}
f.config = c
if len(c.Theme) == 0 {
c.Theme = common.BASE
}
f.Theme = c.Theme
f.Method = c.Method
f.Action = template.HTML(c.Action)
if len(model) > 0 {
f.Model = model[0]
}
return f
}
func (f *Form) HTMLTemplate() (*template.Template, error) {
var tmpl string
if f.config != nil {
tmpl = f.config.Template
}
if len(tmpl) == 0 {
tmpl = common.TmplDir(f.Theme) + `/baseform.html`
//tmpl = common.TmplDir(f.Theme) + `/allfields.html`
}
return common.GetOrSetCachedTemplate(tmpl, func() (*template.Template, error) {
return common.ParseFiles(common.LookupPath(tmpl))
})
}
func (f *Form) Valid(onlyCheckFields ...string) error {
return f.ValidModel(f.Model, onlyCheckFields...)
}
func (f *Form) ValidModel(model interface{}, onlyCheckFields ...string) error {
if model == nil {
return nil
}
passed, err := f.Validate().Valid(model, onlyCheckFields...)
if err != nil {
return err
}
if !passed { // validation does not pass
for index, validErr := range f.Validate().Errors {
if index == 0 {
err = validErr
}
f.Field(validErr.Field).AddError(f.labelFn(validErr.Message))
}
}
return err
}
func (f *Form) InsertErrors() *Form {
if f.valid != nil && f.valid.HasError() {
for _, err := range f.valid.Errors {
f.Field(err.Field).AddError(f.labelFn(err.Message))
}
}
return f
}
func (f *Form) Error() (err *validation.ValidationError) {
if f.valid != nil && f.valid.HasError() {
err = f.valid.Errors[0]
}
return
}
func (f *Form) Errors() (errs []*validation.ValidationError) {
if f.valid != nil && f.valid.HasError() {
errs = f.valid.Errors
}
return
}
func (f *Form) HasError() bool {
return f.valid != nil && f.valid.HasError()
}
func (f *Form) HasErrors() bool {
return f.valid != nil && f.valid.HasErrors()
}
func (f *Form) AddBeforeRender(fn func()) *Form {
if fn == nil {
return f
}
f.beforeRender = append(f.beforeRender, fn)
return f
}
func (f *Form) Validate() *validation.Validation {
if f.valid == nil {
f.valid = &validation.Validation{}
}
return f.valid
}
func (f *Form) SetModel(m interface{}) *Form {
f.Model = m
return f
}
func (f *Form) ParseModel(model ...interface{}) *Form {
var m interface{}
if len(model) > 0 {
m = model[0]
}
if m == nil {
m = f.Model
}
flist, fsort := f.unWindStructure(m, ``)
for _, v := range flist {
f.Elements(v.(config.FormElement))
}
if len(fsort) > 0 {
f.Sort(fsort)
}
return f
}
func (f *Form) AddButton(tmpl string, args ...string) *Form {
btnFields := make([]config.FormElement, 0, 3)
if len(args) < 1 {
btnFields = append(btnFields, fields.SubmitButton("submit", f.labelFn("Submit")))
btnFields = append(btnFields, fields.ResetButton("reset", f.labelFn("Reset")))
} else {
for _, field := range args {
switch field {
case `submit`:
btnFields = append(btnFields, fields.SubmitButton("submit", f.labelFn("Submit")))
case `reset`:
btnFields = append(btnFields, fields.ResetButton("reset", f.labelFn("Reset")))
default:
btnFields = append(btnFields, fields.Button(field, f.labelFn(strings.Title(field))))
}
}
}
if len(tmpl) == 0 {
tmpl = `fieldset_buttons`
}
f.Elements(f.NewFieldSet("_button_group", "", btnFields...).SetTemplate(tmpl))
return f
}
func (f *Form) GenChoicesForField(name string, lenType interface{}, fnType interface{}) *Form {
f.Field(name).SetChoices(f.GenChoices(lenType, fnType))
return f
}
// GenChoices generate choices
//
// type Data struct{
// ID string
// Name string
// }
//
// data:=[]*Data{
// &Data{ID:"a",Name:"One"},
// &Data{ID:"b",Name:"Two"},
// }
//
// GenChoices(len(data), func(index int) (string, string, bool){
// return data[index].ID,data[index].Name,false
// })
//
// or
//
// GenChoices(map[string]int{
// "":len(data),
// }, func(group string,index int) (string, string, bool){
//
// return data[index].ID,data[index].Name,false
// })
func (f *Form) GenChoices(lenType interface{}, fnType interface{}) interface{} {
return GenChoices(lenType, fnType)
}
func (form *Form) unWindStructure(m interface{}, baseName string) ([]interface{}, string) {
t := reflect.TypeOf(m)
v := reflect.ValueOf(m)
if t.Kind() == reflect.Ptr {
t = t.Elem()
v = v.Elem()
}
var (
fieldList []interface{}
fieldSort string
)
fieldSetList := map[string]*FieldSetType{}
fieldSetSort := map[string]string{}
for i := 0; i < t.NumField(); i++ {
if !v.Field(i).CanInterface() {
continue
}
options := make(map[string]struct{})
tag, tagf := common.Tag(t, t.Field(i), "form_options")
if len(tag) > 0 {
var optionsArr []string
if tagf != nil {
cached := tagf.Parsed("form_options", func() interface{} {
return strings.Split(common.TagVal(t, i, "form_options"), ";")
})
optionsArr = cached.([]string)
}
for _, opt := range optionsArr {
if len(opt) > 0 {
options[opt] = struct{}{}
}
}
if _, ok := options["-"]; ok {
continue
}
}
widget := common.TagVal(t, i, "form_widget")
var f fields.FieldInterface
var fName string
if len(baseName) == 0 {
fName = t.Field(i).Name
} else {
fName = strings.Join([]string{baseName, t.Field(i).Name}, ".")
}
useFieldValue := !form.IsOmit(fName)
//fmt.Println(fName, t.Field(i).Type.String(), t.Field(i).Type.Kind())
switch widget {
case "color", "email", "file", "image", "month", "search", "tel", "url", "week":
f = fields.TextFieldFromInstance(v, t, i, fName, useFieldValue, widget)
case "text":
f = fields.TextFieldFromInstance(v, t, i, fName, useFieldValue)
case "hidden":
f = fields.HiddenFieldFromInstance(v, t, i, fName, useFieldValue)
case "textarea":
f = fields.TextAreaFieldFromInstance(v, t, i, fName, useFieldValue)
case "password":
f = fields.PasswordFieldFromInstance(v, t, i, fName, useFieldValue)
case "select":
f = fields.SelectFieldFromInstance(v, t, i, fName, useFieldValue, options, form.labelFn)
case "date":
f = fields.DateFieldFromInstance(v, t, i, fName, useFieldValue)
case "datetime":
f = fields.DatetimeFieldFromInstance(v, t, i, fName, useFieldValue)
case "time":
f = fields.TimeFieldFromInstance(v, t, i, fName, useFieldValue)
case "number":
f = fields.NumberFieldFromInstance(v, t, i, fName, useFieldValue)
case "range":
f = fields.RangeFieldFromInstance(v, t, i, fName, useFieldValue)
case "radio":
f = fields.RadioFieldFromInstance(v, t, i, fName, useFieldValue, form.labelFn)
case "checkbox":
f = fields.CheckboxFieldFromInstance(v, t, i, fName, useFieldValue, form.labelFn)
case "static":
f = fields.StaticFieldFromInstance(v, t, i, fName, useFieldValue)
default:
switch t.Field(i).Type.String() {
case "string":
f = fields.TextFieldFromInstance(v, t, i, fName, useFieldValue)
case "bool":
f = fields.CheckboxFromInstance(v, t, i, fName, useFieldValue, options)
case "time.Time":
f = fields.DatetimeFieldFromInstance(v, t, i, fName, useFieldValue)
case "int", "int64", "float", "float32", "float64":
f = fields.NumberFieldFromInstance(v, t, i, fName, useFieldValue)
case "struct":
fl, fs := form.unWindStructure(v.Field(i).Interface(), fName)
if len(fs) > 0 {
if len(fieldSort) == 0 {
fieldSort = fs
} else {
fieldSort += "," + fs
}
}
fieldList = append(fieldList, fl...)
f = nil
default:
if t.Field(i).Type.Kind() == reflect.Struct ||
(t.Field(i).Type.Kind() == reflect.Ptr &&
t.Field(i).Type.Elem().Kind() == reflect.Struct) {
fl, fs := form.unWindStructure(v.Field(i).Interface(), fName)
if len(fs) > 0 {
if len(fieldSort) == 0 {
fieldSort = fs
} else {
fieldSort += "," + fs
}
}
fieldList = append(fieldList, fl...)
f = nil
} else {
f = fields.TextFieldFromInstance(v, t, i, fName, useFieldValue)
}
}
}
if f != nil {
label := common.TagVal(t, i, "form_label")
if len(label) == 0 {
label = strings.Title(t.Field(i).Name)
} else if label != `-` {
label = form.labelFn(label)
} else {
label = ``
}
f.SetLabel(label)
params := common.TagVal(t, i, "form_params")
if len(params) > 0 {
if paramsMap, err := url.ParseQuery(params); err == nil {
for k, v := range paramsMap {
if k == "placeholder" || k == "title" {
v[0] = form.labelFn(v[0])
}
f.SetParam(k, v[0])
}
} else {
fmt.Println(err)
}
}
valid := common.TagVal(t, i, "valid")
if len(valid) > 0 {
form.validTagFn(valid, f)
}
fieldsetLabel := common.TagVal(t, i, "form_fieldset") // label;name or name
fieldsort := common.TagVal(t, i, "form_sort") // 1 ( or other number ) or "last"
if len(fieldsetLabel) > 0 {
fieldsets := strings.SplitN(fieldsetLabel, ";", 2)
var fieldsetName string
switch len(fieldsets) {
case 1:
fieldsetName = fieldsets[0]
case 2:
fieldsetLabel = fieldsets[0]
fieldsetName = fieldsets[1]
}
fieldsetLabel = form.labelFn(fieldsetLabel)
f.SetData("container", "fieldset")
if _, ok := fieldSetList[fieldsetName]; !ok {
fieldSetList[fieldsetName] = form.NewFieldSet(fieldsetName, fieldsetLabel, f)
} else {
fieldSetList[fieldsetName].Elements(f)
}
if len(fieldsort) > 0 {
if _, ok := fieldSetSort[fieldsetName]; !ok {
fieldSetSort[fieldsetName] = fName + ":" + fieldsort
} else {
fieldSetSort[fieldsetName] += "," + fName + ":" + fieldsort
}
}
} else {
fieldList = append(fieldList, f)
if len(fieldsort) > 0 {
if len(fieldSort) == 0 {
fieldSort = fName + ":" + fieldsort
} else {
fieldSort += "," + fName + ":" + fieldsort
}
}
}
}
}
for _, v := range fieldSetList {
if s, ok := fieldSetSort[v.OriginalName()]; ok {
v.Sort(s)
}
fieldList = append(fieldList, v)
}
return fieldList, fieldSort
}
func (f *Form) SetData(key string, value interface{}) {
f.AppendData[key] = value
}
func (f *Form) Data() map[string]interface{} {
if len(f.data) > 0 {
return f.data
}
safeParams := make(common.HTMLAttributes)
safeParams.FillFromStringMap(f.Params)
f.data = map[string]interface{}{
"container": "",
"fields": f.FieldList,
"groups": config.SplitGroup(f.FieldList),
"classes": f.Class,
"id": f.ID,
"params": safeParams,
"css": f.CSS,
"method": f.Method,
"action": f.Action,
}
for k, v := range f.AppendData {
f.data[k] = v
}
return f.data
}
func (f *Form) runBefore() {
for _, fn := range f.beforeRender {
fn()
}
}
func (f *Form) render() string {
f.runBefore()
buf := bytes.NewBuffer(nil)
t, err := f.HTMLTemplate()
if err != nil {
return err.Error()
}
err = t.Execute(buf, f.Data())
if err != nil {
return err.Error()
}
return buf.String()
}
// Render executes the internal template and renders the form, returning the result as a template.HTML object embeddable
// in any other template.
func (f *Form) Render() template.HTML {
return template.HTML(f.render())
}
func (f *Form) ToHTML(value interface{}) template.HTML {
return template.HTML(fmt.Sprintf("%v", value))
}
func (f *Form) String() string {
return f.render()
}
// Elements adds the provided elements to the form.
func (f *Form) Elements(elems ...config.FormElement) {
for _, e := range elems {
switch v := e.(type) {
case fields.FieldInterface:
f.addField(v)
case *FieldSetType:
f.addFieldSet(v)
case *LangSetType:
f.addLangSet(v)
}
}
}
func (f *Form) addField(field fields.FieldInterface) *Form {
field.SetTheme(f.Theme)
f.FieldList = append(f.FieldList, field)
f.fieldMap[field.OriginalName()] = len(f.FieldList) - 1
return f
}
func (f *Form) addFieldSet(fs *FieldSetType) *Form {
for _, v := range fs.FieldList {
v.SetData("container", "fieldset")
f.containerMap[v.OriginalName()] = fs.OriginalName()
}
f.FieldList = append(f.FieldList, fs)
f.fieldMap[fs.OriginalName()] = len(f.FieldList) - 1
return f
}
func (f *Form) addLangSet(fs *LangSetType) *Form {
for _, v := range fs.fieldMap {
v.SetData("container", "langset")
f.containerMap[v.OriginalName()] = fs.OriginalName()
}
f.FieldList = append(f.FieldList, fs)
f.fieldMap[fs.OriginalName()] = len(f.FieldList) - 1
return f
}
// RemoveElement removes an element (identified by name) from the Form.
func (f *Form) RemoveElement(name string) *Form {
ind, ok := f.fieldMap[name]
if !ok {
return f
}
delete(f.fieldMap, name)
f.FieldList = append(f.FieldList[:ind], f.FieldList[ind+1:]...)
return f
}
// AddClass associates the provided class to the Form.
func (f *Form) AddClass(class string) *Form {
f.Class.Add(class)
return f
}
// RemoveClass removes the given class (if present) from the Form.
func (f *Form) RemoveClass(class string) *Form {
f.Class.Remove(class)
return f
}
// SetID set the given id to the form.
func (f *Form) SetID(id string) *Form {
f.ID = id
return f
}
// SetParam adds the given key-value pair to form parameters list.
func (f *Form) SetParam(key, value string) *Form {
switch key {
case `class`:
f.AddClass(value)
case `action`:
f.Action = template.HTML(value)
default:
f.Params[key] = value
}
return f
}
// DeleteParam removes the parameter identified by key from form parameters list.
func (f *Form) DeleteParam(key string) *Form {
delete(f.Params, key)
return f
}
// AddCSS add a CSS value (in the form of option-value - e.g.: border - auto) to the form.
func (f *Form) AddCSS(key, value string) *Form {
f.CSS[key] = value
return f
}
// RemoveCSS removes CSS style from the form.
func (f *Form) RemoveCSS(key string) *Form {
delete(f.CSS, key)
return f
}
// Field returns the field identified by name. It returns an empty field if it is missing.
func (f *Form) Field(name string) fields.FieldInterface {
ind, ok := f.fieldMap[name]
if !ok {
return &fields.Field{}
}
switch v := f.FieldList[ind].(type) {
case fields.FieldInterface:
return v
case *FieldSetType:
if v, ok := f.containerMap[name]; ok {
return f.FieldSet(v).Field(name)
}
case *LangSetType:
if v, ok := f.containerMap[name]; ok {
return f.LangSet(v).Field(name)
}
}
if f.debug {
fmt.Printf("[Form] Not found field: %s , but has: %#v\n", name, f.fieldMap)
}
return &fields.Field{}
}
// Fields returns all field
func (f *Form) Fields() []config.FormElement {
return f.FieldList
}
// LangSet returns the fieldset identified by name. It returns an empty field if it is missing.
func (f *Form) LangSet(name string) *LangSetType {
ind, ok := f.fieldMap[name]
if !ok {
return &LangSetType{}
}
switch v := f.FieldList[ind].(type) {
case *LangSetType:
return v
default:
return &LangSetType{}
}
}
func (f *Form) NewLangSet(name string, langs []*config.Language) *LangSetType {
return LangSet(name, f.Theme, langs...)
}
// FieldSet returns the fieldset identified by name. It returns an empty field if it is missing.
func (f *Form) FieldSet(name string) *FieldSetType {
ind, ok := f.fieldMap[name]
if !ok {
return &FieldSetType{}
}
switch v := f.FieldList[ind].(type) {
case *FieldSetType:
return v
default:
return &FieldSetType{}
}
}
// NewFieldSet creates and returns a new FieldSetType with the given name and list of fields.
// Every method for FieldSetType objects returns the object itself, so that call can be chained.
func (f *Form) NewFieldSet(name string, label string, elems ...config.FormElement) *FieldSetType {
return FieldSet(name, label, f.Theme, elems...)
}
// SortAll SortAll("field1,field2") or SortAll("field1","field2")
func (f *Form) SortAll(sortList ...string) *Form {
elem := f.FieldList
size := len(elem)
f.FieldList = make([]config.FormElement, size)
var sortSlice []string
if len(sortList) == 1 {
sortSlice = strings.Split(sortList[0], ",")
} else {
sortSlice = sortList
}
for k, fieldName := range sortSlice {
if oldIndex, ok := f.fieldMap[fieldName]; ok {
f.FieldList[k] = elem[oldIndex]
f.fieldMap[fieldName] = k
}
}
return f
}
// Sort Sort("field1:1,field2:2") or Sort("field1:1","field2:2")
func (f *Form) Sort(sortList ...string) *Form {
size := len(f.FieldList)
var sortSlice []string
if len(sortList) == 1 {
sortSlice = strings.Split(sortList[0], ",")
} else {
sortSlice = sortList
}
var index int
endIdx := size - 1
for _, nameIndex := range sortSlice {
ni := strings.Split(nameIndex, ":")
fieldName := ni[0]
if len(ni) > 1 {
if ni[1] == "last" {
index = endIdx
} else if idx, err := strconv.Atoi(ni[1]); err != nil {
continue
} else {
if idx >= 0 {
index = idx
} else {
index = size + idx
}
}
}
if oldIndex, ok := f.fieldMap[fieldName]; ok {
if oldIndex != index && size > index {
f.sortFields(index, oldIndex, endIdx, size)
}
}
index++
}
return f
}
func (f *Form) Sort2Last(fieldsName ...string) *Form {
size := len(f.FieldList)
endIdx := size - 1
index := endIdx
for n := len(fieldsName) - 1; n >= 0; n-- {
fieldName := fieldsName[n]
if oldIndex, ok := f.fieldMap[fieldName]; ok {
if oldIndex != index && index >= 0 {
f.sortFields(index, oldIndex, endIdx, size)
}
}
index--
}
return f
}
func (f *Form) sortFields(index, oldIndex, endIdx, size int) {
var newFields []config.FormElement
oldFields := make([]config.FormElement, size)
copy(oldFields, f.FieldList)
var min, max int
if index > oldIndex {
//[ ][I][ ][ ][ ][ ] I:oldIndex=1
//[ ][ ][ ][ ][I][ ] I:index=4
if oldIndex > 0 {
newFields = oldFields[0:oldIndex]
}
newFields = append(newFields, oldFields[oldIndex+1:index+1]...)
newFields = append(newFields, f.FieldList[oldIndex])
if index+1 <= endIdx {
newFields = append(newFields, f.FieldList[index+1:]...)
}
min = oldIndex
max = index
} else {
//[ ][ ][ ][ ][I][ ] I:oldIndex=4
//[ ][I][ ][ ][ ][ ] I:index=1
if index > 0 {
newFields = oldFields[0:index]
}
newFields = append(newFields, oldFields[oldIndex])
newFields = append(newFields, f.FieldList[index:oldIndex]...)
if oldIndex+1 <= endIdx {
newFields = append(newFields, f.FieldList[oldIndex+1:]...)
}
min = index
max = oldIndex
}
for i := min; i <= max; i++ {
f.fieldMap[newFields[i].OriginalName()] = i
}
f.FieldList = newFields
}