refactor: clean up form binding & validation (#38873)

Clarify the "validation" and "error display" logic.

All the copied&pasted `Validate` functions are removed.
This commit is contained in:
wxiaoguang
2026-08-14 10:15:33 +08:00
committed by GitHub
parent 8b40df255b
commit 72a9debaff
30 changed files with 331 additions and 737 deletions
+4 -23
View File
@@ -5,13 +5,8 @@
package forms
import (
"net/http"
"gitea.dev/modules/structs"
"gitea.dev/modules/web/middleware"
"gitea.dev/services/context"
"gitea.com/go-chi/binding"
)
// ________ .__ __ .__
@@ -23,19 +18,15 @@ import (
// CreateOrgForm form for creating organization
type CreateOrgForm struct {
middleware.FormDefaultValidator
OrgName string `binding:"Required;Username;MaxSize(40)" locale:"org.org_name_holder"`
Visibility structs.VisibleType
RepoAdminChangeTeamAccess bool
}
// Validate validates the fields
func (f *CreateOrgForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
// UpdateOrgSettingForm form for updating organization settings
type UpdateOrgSettingForm struct {
middleware.FormDefaultValidator
FullName *string `binding:"MaxSize(100)"`
Email *string `binding:"MaxSize(255)"`
Description *string `binding:"MaxSize(255)"`
@@ -45,13 +36,8 @@ type UpdateOrgSettingForm struct {
RepoAdminChangeTeamAccess *bool
}
// Validate validates the fields
func (f *UpdateOrgSettingForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}
type RenameOrgForm struct {
middleware.FormDefaultValidator
OrgName string `binding:"Required"`
NewOrgName string `binding:"Required;Username;MaxSize(40)" locale:"org.org_name_holder"`
}
@@ -65,6 +51,7 @@ type RenameOrgForm struct {
// CreateTeamForm form for creating team
type CreateTeamForm struct {
middleware.FormDefaultValidator
TeamName string `binding:"Required;AlphaDashDot;MaxSize(255)"`
Description string `binding:"MaxSize(255)"`
Permission string
@@ -72,9 +59,3 @@ type CreateTeamForm struct {
CanCreateOrgRepo bool
Visibility string `binding:"OmitEmpty;In(public,limited,private)"`
}
// Validate validates the fields
func (f *CreateTeamForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(ctx, errs, f)
}