Skip to content

Commit ff359e0

Browse files
committed
improved
1 parent 1fe97cc commit ff359e0

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,17 @@ This category includes checkbox, select and radio button fields.
255255
Checkbox field requires a name and a set of options to populate the field. The options are just a set of InputChoice (Id-Value pairs) objects:
256256

257257
opts := []fields.InputChoice{
258-
fields.InputChoice{"A", "Option A"},
259-
fields.InputChoice{"B", "Option B"},
258+
fields.InputChoice{Id:"A", Val:"Option A"},
259+
fields.InputChoice{Id:"B", Val:"Option B"},
260260
}
261261
f := fields.CheckboxField("checkbox", opts)
262262
f.AddSelected("A", "B")
263263

264264
Radio buttons, instead, require a name and a set of options to populate the field. The options are just a set of InputChoice (Id-Value pairs) objects:
265265

266266
opts := []fields.InputChoice{
267-
fields.InputChoice{"A", "Option A"},
268-
fields.InputChoice{"B", "Option B"},
267+
fields.InputChoice{Id:"A", Val:"Option A"},
268+
fields.InputChoice{Id:"B", Val:"Option B"},
269269
}
270270
f := fields.RadioField("radio", opts)
271271

@@ -274,8 +274,8 @@ Select fields, on the other hand, allow option grouping. This can be achieved by
274274
opts := map[string][]fields.InputChoice{
275275
"": []fields.InputChoice{fields.InputChoice{"A", "Option A"}},
276276
"group1": []fields.InputChoice{
277-
fields.InputChoice{"B", "Option B"},
278-
fields.InputChoice{"C", "Option C"},
277+
fields.InputChoice{Id:"B", Val:"Option B"},
278+
fields.InputChoice{Id:"C", Val:"Option C"},
279279
}
280280
}
281281
f := fields.SelectField("select", opts)

fields/field.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,13 @@ func (f *Field) RemoveSelected(opt string) FieldInterface {
373373
func (f *Field) SetChoices(choices interface{}, saveIndex ...bool) FieldInterface {
374374
switch f.fieldType {
375375
case formcommon.SELECT:
376-
ch, _ := choices.(map[string][]InputChoice)
376+
var ch map[string][]InputChoice
377+
if c, ok := choices.(map[string][]InputChoice); ok {
378+
ch = c
379+
}else{
380+
c, _ := choices.([]InputChoice)
381+
ch = map[string][]InputChoice{"":c}
382+
}
377383
f.choices = ch
378384
if len(saveIndex) < 1 || saveIndex[0] {
379385
for k, v := range ch {

0 commit comments

Comments
 (0)