feat(field): add IsFixedName property and WithFixedName method
This commit is contained in:
parent
36b80e1e20
commit
2cee3a1f92
1 changed files with 21 additions and 3 deletions
|
|
@ -49,15 +49,17 @@ type Field struct {
|
|||
BeforeBind func(data any) (any, error)
|
||||
Validate func(f *Field) bool
|
||||
IsSlice bool
|
||||
IsFixedName bool
|
||||
Form *Form
|
||||
Parent *Field
|
||||
}
|
||||
|
||||
func NewField(name, widget string) *Field {
|
||||
f := &Field{
|
||||
Name: name,
|
||||
Widget: widget,
|
||||
Data: nil,
|
||||
Name: name,
|
||||
IsFixedName: false,
|
||||
Widget: widget,
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
f.PrepareView = func() map[string]any {
|
||||
|
|
@ -111,6 +113,12 @@ func (f *Field) WithOptions(options ...*Option) *Field {
|
|||
return f
|
||||
}
|
||||
|
||||
func (f *Field) WithData(data any) *Field {
|
||||
f.Data = data
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *Field) ResetErrors() *Field {
|
||||
f.Errors = []validation.Error{}
|
||||
|
||||
|
|
@ -123,6 +131,12 @@ func (f *Field) WithSlice() *Field {
|
|||
return f
|
||||
}
|
||||
|
||||
func (f *Field) WithFixedName() *Field {
|
||||
f.IsFixedName = true
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *Field) WithConstraints(constraints ...validation.Constraint) *Field {
|
||||
for _, constraint := range constraints {
|
||||
f.Constraints = append(f.Constraints, constraint)
|
||||
|
|
@ -179,6 +193,10 @@ func (f *Field) GetChild(name string) *Field {
|
|||
func (f *Field) GetName() string {
|
||||
var name string
|
||||
|
||||
if f.IsFixedName {
|
||||
return f.Name
|
||||
}
|
||||
|
||||
if f.Form != nil && f.Form.Name != "" {
|
||||
name = fmt.Sprintf("%s[%s]", f.Form.Name, f.Name)
|
||||
} else if f.Parent != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue