A standalone library for binding nested form data to Go structs. This library provides advanced form binding capabilities with support for nested structures, arrays, and pointer fields.
- Nested Struct Binding: Bind deeply nested structures from form data
- Array Support: Handle array notation with indices (e.g.,
items[0].name) - Pointer Field Support: Automatically handle pointer fields and nil checks
- Dot Notation: Support for nested field access using dot notation
- Sparse Arrays: Handle non-sequential array indices gracefully
- Zero Dependencies: No external dependencies beyond Go standard library
go get github.com/labstack/echo-contrib/formbindpackage main
import (
"fmt"
"net/url"
"github.com/labstack/echo-contrib/formbind"
)
type Person struct {
Name string `form:"name"`
Email string `form:"email"`
}
type Team struct {
Name string `form:"name"`
Members []Person `form:"members"`
}
func main() {
// Parse form data
formData := url.Values{
"name": {"Engineering Team"},
"members[0].name": {"Alice"},
"members[0].email": {"alice@example.com"},
"members[1].name": {"Bob"},
"members[1].email": {"bob@example.com"},
}
var team Team
err := formbind.Bind(&team, formData)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", team)
// Output: {Name:Engineering Team Members:[{Name:Alice Email:alice@example.com} {Name:Bob Email:bob@example.com}]}
}func Bind(dst interface{}, data url.Values) errorBinds form data to the destination struct. The destination must be a pointer to a struct.
stringint,int8,int16,int32,int64uint,uint8,uint16,uint32,uint64float32,float64booltime.Time(with custom time format support)- Slices of above types
- Nested structs
- Pointers to any of the above types
The library uses the form tag to map form fields to struct fields:
type User struct {
Name string `form:"name"`
Email string `form:"email_address"`
}If no form tag is provided, the field name is used (case-insensitive matching).
The library returns descriptive errors for common issues:
var data FormData
err := formbind.Bind(&data, formValues)
if err != nil {
// Handle specific error types
switch err.(type) {
case *formbind.BindError:
// Field-specific binding error
case *formbind.ParseError:
// Value parsing error
default:
// Other errors
}
}This library is based on the nested form binding implementation proposed in Echo PR #2834, extracted as a standalone library for broader use.