From d5062b252fc5db4ecc1c5ce5a5a9339e8a1ca414 Mon Sep 17 00:00:00 2001 From: Sebastiano Bellinzis Date: Thu, 8 Feb 2024 23:30:57 +0100 Subject: [PATCH] minor fixes and remove debug line --- .gitignore | 1 + form.go | 6 ++++++ transformer.go | 18 +++++++++++++----- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index d9592b4..3dac912 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ .idea/ +example diff --git a/form.go b/form.go index 8921d7d..1dde931 100644 --- a/form.go +++ b/form.go @@ -74,6 +74,12 @@ func (f *Form) formRender(v interface{}, errs []FieldError, kv ...any) (template return subhtml }, + "errors": func() []string { + if errs, ok := fieldErrors[field.Name]; ok { + return errs + } + return nil + }, }) fMap := copyMap(data) diff --git a/transformer.go b/transformer.go index 27e770d..f969de9 100644 --- a/transformer.go +++ b/transformer.go @@ -69,8 +69,6 @@ func NewTransformer(model interface{}) (*Transformer, error) { tr.Fields = fields - fmt.Printf("tr: %+v\n", tr.Fields) - return tr, nil } @@ -88,12 +86,12 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names tags := rType.Field(i).Tag name := tags.Get(tagName) + fieldName := rType.Field(i).Name if name == "" { - name = rType.Field(i).Name + name = fieldName } nname := append(names, name) - field := FormField{ Label: tags.Get(tagLabel), Placeholder: tags.Get(tagPlaceholder), @@ -228,7 +226,17 @@ func (t *Transformer) scanModel(rValue reflect.Value, rType reflect.Type, names field.Step = "any" } case reflect.Bool: - field.Type = FieldTypeCheckbox + fieldType := FieldTypeCheckbox + if len(names) > 0 && names[len(names)-1] == name { + // radio-options use the same 'name' as their parent for grouping + fieldType = FieldTypeRadios + } + + field.Type = fieldType + + // replace last slice element with the field name + nname[len(nname)-1] = fieldName + field.Id = strings.Join(nname, ".") case reflect.Slice, reflect.Array: case reflect.Map: case reflect.Struct: