mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-16 11:04:59 +00:00
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:
@@ -5,12 +5,11 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"gitea.dev/modules/reqctx"
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/structs"
|
||||
"gitea.dev/modules/translation"
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/modules/validation"
|
||||
@@ -18,17 +17,13 @@ import (
|
||||
"gitea.com/go-chi/binding"
|
||||
)
|
||||
|
||||
// ValidateContext is a special context for form validation middleware. It may be different from other contexts.
|
||||
type ValidateContext struct {
|
||||
Locale translation.Locale
|
||||
Data reqctx.ContextData
|
||||
Req *http.Request
|
||||
Resp http.ResponseWriter
|
||||
}
|
||||
type (
|
||||
ValidateContext = structs.ValidateContext
|
||||
FormDefaultValidator = structs.FormDefaultValidator
|
||||
)
|
||||
|
||||
// Form form binding interface
|
||||
type Form interface {
|
||||
binding.Validator
|
||||
Validate(ctx *ValidateContext, errs binding.Errors) binding.Errors
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -84,9 +79,17 @@ func getFieldDisplayNameForMessage(f Form, l translation.Locale, fieldNames []st
|
||||
typ = typ.Elem()
|
||||
}
|
||||
|
||||
field, fieldExists := typ.FieldByName(fieldNames[0])
|
||||
fieldName := fieldNames[0]
|
||||
field, fieldExists := typ.FieldByName(fieldName)
|
||||
if !fieldExists {
|
||||
return field, false, ""
|
||||
for tryField := range typ.Fields() {
|
||||
if util.ToSnakeCase(tryField.Name) == fieldName || tryField.Tag.Get("form") == fieldName {
|
||||
field, fieldExists = tryField, true
|
||||
}
|
||||
}
|
||||
if !fieldExists {
|
||||
return field, false, ""
|
||||
}
|
||||
}
|
||||
|
||||
if field.Tag.Get("form") == "-" {
|
||||
@@ -95,8 +98,9 @@ func getFieldDisplayNameForMessage(f Form, l translation.Locale, fieldNames []st
|
||||
|
||||
trKeyFallback := "form." + field.Name
|
||||
trKey := util.IfZero(field.Tag.Get("locale"), trKeyFallback)
|
||||
displayName = l.TrString(trKey)
|
||||
if displayName == trKeyFallback {
|
||||
if l.HasKey(trKey) {
|
||||
displayName = l.TrString(trKey)
|
||||
} else {
|
||||
displayName = field.Name
|
||||
}
|
||||
return field, true, displayName
|
||||
@@ -156,7 +160,7 @@ func BuildValidationErrorForUser(f Form, l translation.Locale, bindingErrs bindi
|
||||
case validation.ErrInvalidBadgeSlug:
|
||||
errorMessage = l.TrString("form.invalid_slug_error", fieldDisplayName)
|
||||
default:
|
||||
setting.PanicInDevOrTesting("unknown binding error classification: %v", classification)
|
||||
setting.PanicInDevOrTesting("unknown binding error classification for field %T.%s: %v, err: %s", f, errorFieldName, classification, bindingErrMsg)
|
||||
var msg string
|
||||
if classification != "" && bindingErrMsg != "" {
|
||||
msg = classification + ": " + bindingErrMsg
|
||||
@@ -171,33 +175,3 @@ func BuildValidationErrorForUser(f Form, l translation.Locale, bindingErrs bindi
|
||||
}
|
||||
return errorMessage, errorFieldName, fieldNames
|
||||
}
|
||||
|
||||
type contextKeySkipTmplFormValidationErrorType struct{}
|
||||
|
||||
var contextKeySkipTmplFormValidationError contextKeySkipTmplFormValidationErrorType
|
||||
|
||||
func SkipTmplFormValidationError(ctx reqctx.RequestContext) {
|
||||
ctx.SetContextValue(contextKeySkipTmplFormValidationError, true)
|
||||
}
|
||||
|
||||
func Validate(ctx *ValidateContext, errs binding.Errors, f Form) binding.Errors {
|
||||
if ctx.Req.Context().Value(contextKeySkipTmplFormValidationError) == true {
|
||||
// if it is not using tmpl-based validation error handling, just return the errors
|
||||
// for example: when using "form-fetch-action", the validation error can be handled by GetFetchActionForm
|
||||
return errs
|
||||
}
|
||||
errorMessage, errorFieldName, _ := BuildValidationErrorForUser(f, ctx.Locale, errs)
|
||||
if errorMessage == "" {
|
||||
return errs
|
||||
}
|
||||
|
||||
// Legacy template error handling: try to restore the form's values as much as possible,
|
||||
// especially for RenderWithErrDeprecated to re-render the form with errors.
|
||||
AssignForm(f, ctx.Data)
|
||||
ctx.Data["HasError"] = true
|
||||
ctx.Data["ErrorMsg"] = errorMessage
|
||||
if errorFieldName != "" {
|
||||
ctx.Data["Err_"+errorFieldName] = true
|
||||
}
|
||||
return errs
|
||||
}
|
||||
|
||||
@@ -15,17 +15,16 @@ import (
|
||||
)
|
||||
|
||||
type testRangeForm struct {
|
||||
FormDefaultValidator
|
||||
Hours int `binding:"Range(0,1000)"`
|
||||
}
|
||||
|
||||
func (f *testRangeForm) Validate(_ *http.Request, errs binding.Errors) binding.Errors { return errs }
|
||||
|
||||
func TestBuildValidationErrorForUser(t *testing.T) {
|
||||
// an out-of-range value must reach its own message instead of the panicking "default" branch
|
||||
form := &testRangeForm{Hours: 2000}
|
||||
errs := binding.Validate(httptest.NewRequest(http.MethodPost, "/", nil), form)
|
||||
errorMessage, errorFieldName, fieldNames := BuildValidationErrorForUser(form, translation.MockLocale{}, errs)
|
||||
assert.Equal(t, "form.range_error:Hours,0,1000", errorMessage)
|
||||
assert.Equal(t, "form.range_error:form.Hours,0,1000", errorMessage)
|
||||
assert.Equal(t, "Hours", errorFieldName)
|
||||
assert.Equal(t, []string{"Hours"}, fieldNames)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user