diff --git a/form/field.go b/form/field.go index bce6188..f6b9177 100644 --- a/form/field.go +++ b/form/field.go @@ -8,6 +8,7 @@ import ( "gitnet.fr/deblan/go-form/validation" ) +// Generic function for field.Validation func FieldValidation(f *Field) bool { if len(f.Children) > 0 { isValid := true @@ -36,6 +37,7 @@ func FieldValidation(f *Field) bool { } } +// Field represents a field in a form type Field struct { Name string Widget string @@ -54,6 +56,8 @@ type Field struct { Parent *Field } +// Generates a new field with default properties +// It should not be used directly but inside function like in form.NewFieldText func NewField(name, widget string) *Field { f := &Field{ Name: name, @@ -81,6 +85,7 @@ func NewField(name, widget string) *Field { return f } +// Checks if the field contains an option using its name func (f *Field) HasOption(name string) bool { for _, option := range f.Options { if option.Name == name { @@ -91,6 +96,7 @@ func (f *Field) HasOption(name string) bool { return false } +// Returns an option using its name func (f *Field) GetOption(name string) *Option { for _, option := range f.Options { if option.Name == name { @@ -101,6 +107,7 @@ func (f *Field) GetOption(name string) *Option { return nil } +// Appends options to the field func (f *Field) WithOptions(options ...*Option) *Field { for _, option := range options { if f.HasOption(option.Name) { @@ -113,30 +120,35 @@ func (f *Field) WithOptions(options ...*Option) *Field { return f } +// Sets data the field func (f *Field) WithData(data any) *Field { f.Data = data return f } +// Resets the field errors func (f *Field) ResetErrors() *Field { f.Errors = []validation.Error{} return f } +// Sets that the field represents a data slice func (f *Field) WithSlice() *Field { f.IsSlice = true return f } +// Sets that the name of the field is not computed func (f *Field) WithFixedName() *Field { f.IsFixedName = true return f } +// Appends constraints func (f *Field) WithConstraints(constraints ...validation.Constraint) *Field { for _, constraint := range constraints { f.Constraints = append(f.Constraints, constraint) @@ -145,18 +157,21 @@ func (f *Field) WithConstraints(constraints ...validation.Constraint) *Field { return f } +// Sets a transformer applied to the structure data before displaying it in a field func (f *Field) WithBeforeMount(callback func(data any) (any, error)) *Field { f.BeforeMount = callback return f } +// Sets a transformer applied to the data of a field before defining it in a structure func (f *Field) WithBeforeBind(callback func(data any) (any, error)) *Field { f.BeforeBind = callback return f } +// Appends children func (f *Field) Add(children ...*Field) *Field { for _, child := range children { child.Parent = f @@ -166,6 +181,7 @@ func (f *Field) Add(children ...*Field) *Field { return f } +// Checks if the field contains a child using its name func (f *Field) HasChild(name string) bool { for _, child := range f.Children { if name == child.Name { @@ -176,6 +192,7 @@ func (f *Field) HasChild(name string) bool { return false } +// Returns a child using its name func (f *Field) GetChild(name string) *Field { var result *Field @@ -190,6 +207,7 @@ func (f *Field) GetChild(name string) *Field { return result } +// Computes the name of the field func (f *Field) GetName() string { var name string @@ -208,6 +226,7 @@ func (f *Field) GetName() string { return name } +// Computes the id of the field func (f *Field) GetId() string { name := f.GetName() name = strings.ReplaceAll(name, "[", "-") @@ -217,6 +236,7 @@ func (f *Field) GetId() string { return name } +// Populates the field with data func (f *Field) Mount(data any) error { data, err := f.BeforeMount(data) @@ -249,6 +269,7 @@ func (f *Field) Mount(data any) error { return nil } +// Bind the data into the given map func (f *Field) Bind(data map[string]any, key *string) error { if len(f.Children) == 0 { v, err := f.BeforeBind(f.Data) diff --git a/form/field_checkbox.go b/form/field_checkbox.go index 7fc3947..d8a92dc 100644 --- a/form/field_checkbox.go +++ b/form/field_checkbox.go @@ -4,6 +4,7 @@ import ( "github.com/spf13/cast" ) +// Generates an input[type=checkbox] func NewFieldCheckbox(name string) *Field { f := NewField(name, "input"). WithOptions(NewOption("type", "checkbox")). diff --git a/form/field_choice.go b/form/field_choice.go index cbdc170..c134343 100644 --- a/form/field_choice.go +++ b/form/field_choice.go @@ -80,6 +80,7 @@ func (c *Choices) GetChoices() []Choice { return choices } +// Generates an instance of Choices func NewChoices(items any) *Choices { builder := func(key int, item any) string { return cast.ToString(key) @@ -94,6 +95,7 @@ func NewChoices(items any) *Choices { return &choices } +// Generates inputs (checkbox or radio) or selects func NewFieldChoice(name string) *Field { f := NewField(name, "choice"). WithOptions( diff --git a/form/field_input.go b/form/field_input.go index 2bc5a7e..9a1eb33 100644 --- a/form/field_input.go +++ b/form/field_input.go @@ -4,6 +4,7 @@ import ( "github.com/spf13/cast" ) +// Generates an input[type=text] func NewFieldText(name string) *Field { f := NewField(name, "input"). WithOptions(NewOption("type", "text")) @@ -11,6 +12,7 @@ func NewFieldText(name string) *Field { return f } +// Generates an input[type=number] with default transformers func NewFieldNumber(name string) *Field { f := NewField(name, "input"). WithOptions(NewOption("type", "number")). @@ -21,6 +23,7 @@ func NewFieldNumber(name string) *Field { return f } +// Generates an input[type=email] func NewFieldMail(name string) *Field { f := NewField(name, "input"). WithOptions(NewOption("type", "email")) @@ -28,6 +31,7 @@ func NewFieldMail(name string) *Field { return f } +// Generates an input[type=range] func NewFieldRange(name string) *Field { f := NewField(name, "input"). WithOptions(NewOption("type", "range")). @@ -38,6 +42,7 @@ func NewFieldRange(name string) *Field { return f } +// Generates an input[type=password] func NewFieldPassword(name string) *Field { f := NewField(name, "input"). WithOptions(NewOption("type", "password")) @@ -45,6 +50,7 @@ func NewFieldPassword(name string) *Field { return f } +// Generates an input[type=hidden] func NewFieldHidden(name string) *Field { f := NewField(name, "input"). WithOptions(NewOption("type", "hidden")) @@ -52,6 +58,7 @@ func NewFieldHidden(name string) *Field { return f } +// Generates an input[type=submit] func NewSubmit(name string) *Field { f := NewField(name, "input"). WithOptions(NewOption("type", "submit")) diff --git a/form/field_input_date.go b/form/field_input_date.go index 5a4a257..ee21e6b 100644 --- a/form/field_input_date.go +++ b/form/field_input_date.go @@ -25,6 +25,7 @@ func DateBeforeMount(data any, format string) (any, error) { return data, nil } +// Generates an input[type=date] with default transformers func NewFieldDate(name string) *Field { f := NewField(name, "input"). WithOptions(NewOption("type", "date")). @@ -38,6 +39,7 @@ func NewFieldDate(name string) *Field { return f } +// Generates an input[type=datetime] with default transformers func NewFieldDatetime(name string) *Field { f := NewField(name, "input"). WithOptions(NewOption("type", "datetime")). @@ -51,6 +53,7 @@ func NewFieldDatetime(name string) *Field { return f } +// Generates an input[type=datetime-local] with default transformers func NewFieldDatetimeLocal(name string) *Field { f := NewField(name, "input"). WithOptions( @@ -68,6 +71,7 @@ func NewFieldDatetimeLocal(name string) *Field { return f } +// Generates an input[type=time] with default transformers func NewFieldTime(name string) *Field { f := NewField(name, "input"). WithOptions(NewOption("type", "time")). diff --git a/form/field_subform.go b/form/field_subform.go index 9c5f21c..bc439ae 100644 --- a/form/field_subform.go +++ b/form/field_subform.go @@ -1,5 +1,6 @@ package form +// Generates a sub form func NewFieldSubForm(name string) *Field { f := NewField(name, "sub_form") diff --git a/form/field_textarea.go b/form/field_textarea.go index c32f704..09156ff 100644 --- a/form/field_textarea.go +++ b/form/field_textarea.go @@ -1,5 +1,6 @@ package form +// Generates a textarea func NewFieldTextarea(name string) *Field { return NewField(name, "textarea") }