From 56c7ac9d048fd36c1cae28ce656d4e00fd6ba89b Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Mon, 18 Aug 2025 09:45:38 +0200 Subject: [PATCH] feat: add json configuration --- form/field.go | 28 ++++++++++++++-------------- form/field_choice.go | 25 ++++++++++++++++++++++--- form/form.go | 18 +++++++++--------- form/option.go | 4 ++-- 4 files changed, 47 insertions(+), 28 deletions(-) diff --git a/form/field.go b/form/field.go index 90b5154..1a3f5e5 100644 --- a/form/field.go +++ b/form/field.go @@ -56,20 +56,20 @@ func FieldValidation(f *Field) bool { // Field represents a field in a form type Field struct { - Name string - Widget string - Data any - Options []*Option - Children []*Field - Constraints []validation.Constraint - Errors []validation.Error - BeforeMount func(data any) (any, error) - BeforeBind func(data any) (any, error) - Validate func(f *Field) bool - IsSlice bool - IsFixedName bool - Form *Form - Parent *Field + Name string `json:"name"` + Widget string `json:"widget"` + Data any `json:"-"` + Options []*Option `json:"options"` + Children []*Field `json:"children"` + Constraints []validation.Constraint `json:"-"` + Errors []validation.Error `json:"-"` + BeforeMount func(data any) (any, error) `json:"-"` + BeforeBind func(data any) (any, error) `json:"-"` + Validate func(f *Field) bool `json:"-"` + IsSlice bool `json:"is_slice"` + IsFixedName bool `json:"is_fixed_name"` + Form *Form `json:"-"` + Parent *Field `json:"-"` } // Generates a new field with default properties diff --git a/form/field_choice.go b/form/field_choice.go index 5572344..70d2545 100644 --- a/form/field_choice.go +++ b/form/field_choice.go @@ -16,6 +16,7 @@ package form // along with this program. If not, see . import ( + "encoding/json" "reflect" "github.com/spf13/cast" @@ -33,9 +34,9 @@ func (c Choice) Match(value string) bool { } type Choices struct { - Data any - ValueBuilder func(key int, item any) string - LabelBuilder func(key int, item any) string + Data any `json:"data"` + ValueBuilder func(key int, item any) string `json:"-"` + LabelBuilder func(key int, item any) string `json:"-"` } func (c *Choices) Match(f *Field, value string) bool { @@ -100,6 +101,24 @@ func (c *Choices) GetChoices() []Choice { return choices } +func (c Choices) MarshalJSON() ([]byte, error) { + var choices []map[string]string + + v := reflect.ValueOf(c.Data) + + switch v.Kind() { + case reflect.Slice, reflect.Array, reflect.String, reflect.Map: + for i := 0; i < v.Len(); i++ { + choices = append(choices, map[string]string{ + "value": c.ValueBuilder(i, v.Index(i).Interface()), + "label": c.LabelBuilder(i, v.Index(i).Interface()), + }) + } + } + + return json.Marshal(choices) +} + // Generates an instance of Choices func NewChoices(items any) *Choices { builder := func(key int, item any) string { diff --git a/form/form.go b/form/form.go index 85325fa..a2dda2a 100644 --- a/form/form.go +++ b/form/form.go @@ -30,15 +30,15 @@ import ( // Field represents a form type Form struct { - Fields []*Field - GlobalFields []*Field - Errors []validation.Error - Method string - JsonRequest bool - Action string - Name string - Options []*Option - RequestData *url.Values + Fields []*Field `json:"children"` + GlobalFields []*Field `json:"-"` + Errors []validation.Error `json:"-"` + Method string `json:"method"` + JsonRequest bool `json:"json_request"` + Action string `json:"action"` + Name string `json:"name"` + Options []*Option `json:"options"` + RequestData *url.Values `json:"-"` } // Generates a new form with default properties diff --git a/form/option.go b/form/option.go index 9fccd27..95eee67 100644 --- a/form/option.go +++ b/form/option.go @@ -18,8 +18,8 @@ import "strings" // along with this program. If not, see . type Option struct { - Name string - Value any + Name string `json:"name"` + Value any `json:"value"` } func NewOption(name string, value any) *Option {