feat: add json configuration

This commit is contained in:
Simon Vieille 2025-08-18 09:45:38 +02:00
commit 56c7ac9d04
Signed by: deblan
GPG key ID: 579388D585F70417
4 changed files with 47 additions and 28 deletions

View file

@ -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

View file

@ -16,6 +16,7 @@ package form
// along with this program. If not, see <http://www.gnu.org/licenses/>.
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 {

View file

@ -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

View file

@ -18,8 +18,8 @@ import "strings"
// along with this program. If not, see <http://www.gnu.org/licenses/>.
type Option struct {
Name string
Value any
Name string `json:"name"`
Value any `json:"value"`
}
func NewOption(name string, value any) *Option {