refactor: form binding validation (#38832)

Make "form-fetch-action" highlight the invalid field as "error"
This commit is contained in:
wxiaoguang
2026-08-10 01:25:31 +00:00
committed by GitHub
parent b5aa8c4ff0
commit 9c915e4e1a
27 changed files with 411 additions and 323 deletions
+5 -1
View File
@@ -26,7 +26,7 @@ func Bind[T any](_ T) http.HandlerFunc {
return func(resp http.ResponseWriter, req *http.Request) {
theObj := new(T) // create a new form obj for every request but not use obj directly
data := middleware.GetContextData(req.Context())
binding.Bind(req, theObj)
_ = binding.Bind(req, theObj) // no need to handle "errs" here, the errors are handled in our middleware.Validate (binding.go)
SetForm(data, theObj)
middleware.AssignForm(theObj, data)
}
@@ -37,6 +37,10 @@ func SetForm(dataStore reqctx.ContextDataProvider, obj any) {
dataStore.GetData()["__form"] = obj
}
func IsFormSet(dataStore reqctx.RequestDataStore) bool {
return dataStore.GetData()["__form"] != nil
}
// GetForm returns the validate form information
func GetForm[T any](dataStore reqctx.RequestDataStore) T {
form, ok := dataStore.GetData()["__form"].(T)