41 lines
773 B
Go
41 lines
773 B
Go
package form
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"management/internal/pkg/validation"
|
|
|
|
"github.com/gorilla/schema"
|
|
)
|
|
|
|
// 初始化一个解码器实例
|
|
var decoder = schema.NewDecoder()
|
|
|
|
func BindForm(r *http.Request, form any) error {
|
|
if err := r.ParseForm(); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 忽略名为 "csrf_token" 的表单字段
|
|
delete(r.PostForm, "csrf_token")
|
|
|
|
// decoder.RegisterConverter(time.Time{}, func(s string) reflect.Value {
|
|
// res, err := time.ParseInLocation("2006-01-02", s, time.Local)
|
|
// if err != nil {
|
|
// panic(err)
|
|
// }
|
|
// return reflect.ValueOf(res)
|
|
// })
|
|
|
|
if err := decoder.Decode(form, r.PostForm); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 校验表单规则
|
|
if err := validation.ValidateForm(form); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|