mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
3.9 KiB
146 lines
3.9 KiB
11 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
6 years ago
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||
11 years ago
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
4 years ago
|
package middlewares
|
||
11 years ago
|
|
||
|
import (
|
||
|
"reflect"
|
||
10 years ago
|
"strings"
|
||
11 years ago
|
|
||
4 years ago
|
"code.gitea.io/gitea/modules/translation"
|
||
8 years ago
|
"code.gitea.io/gitea/modules/validation"
|
||
5 years ago
|
|
||
4 years ago
|
"gitea.com/go-chi/binding"
|
||
5 years ago
|
"github.com/unknwon/com"
|
||
11 years ago
|
)
|
||
|
|
||
8 years ago
|
// Form form binding interface
|
||
10 years ago
|
type Form interface {
|
||
|
binding.Validator
|
||
|
}
|
||
|
|
||
10 years ago
|
func init() {
|
||
|
binding.SetNameMapper(com.ToSnakeCase)
|
||
|
}
|
||
|
|
||
10 years ago
|
// AssignForm assign form values back to the template data.
|
||
|
func AssignForm(form interface{}, data map[string]interface{}) {
|
||
|
typ := reflect.TypeOf(form)
|
||
|
val := reflect.ValueOf(form)
|
||
|
|
||
4 years ago
|
for typ.Kind() == reflect.Ptr {
|
||
10 years ago
|
typ = typ.Elem()
|
||
|
val = val.Elem()
|
||
|
}
|
||
|
|
||
|
for i := 0; i < typ.NumField(); i++ {
|
||
|
field := typ.Field(i)
|
||
|
|
||
|
fieldName := field.Tag.Get("form")
|
||
|
// Allow ignored fields in the struct
|
||
|
if fieldName == "-" {
|
||
|
continue
|
||
10 years ago
|
} else if len(fieldName) == 0 {
|
||
|
fieldName = com.ToSnakeCase(field.Name)
|
||
10 years ago
|
}
|
||
|
|
||
|
data[fieldName] = val.Field(i).Interface()
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
func getRuleBody(field reflect.StructField, prefix string) string {
|
||
10 years ago
|
for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
|
||
10 years ago
|
if strings.HasPrefix(rule, prefix) {
|
||
8 years ago
|
return rule[len(prefix) : len(rule)-1]
|
||
10 years ago
|
}
|
||
|
}
|
||
|
return ""
|
||
|
}
|
||
|
|
||
8 years ago
|
// GetSize get size int form tag
|
||
9 years ago
|
func GetSize(field reflect.StructField) string {
|
||
9 years ago
|
return getRuleBody(field, "Size(")
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// GetMinSize get minimal size in form tag
|
||
10 years ago
|
func GetMinSize(field reflect.StructField) string {
|
||
9 years ago
|
return getRuleBody(field, "MinSize(")
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
// GetMaxSize get max size in form tag
|
||
10 years ago
|
func GetMaxSize(field reflect.StructField) string {
|
||
9 years ago
|
return getRuleBody(field, "MaxSize(")
|
||
|
}
|
||
|
|
||
8 years ago
|
// GetInclude get include in form tag
|
||
9 years ago
|
func GetInclude(field reflect.StructField) string {
|
||
|
return getRuleBody(field, "Include(")
|
||
10 years ago
|
}
|
||
|
|
||
4 years ago
|
// Validate validate TODO:
|
||
|
func Validate(errs binding.Errors, data map[string]interface{}, f Form, l translation.Locale) binding.Errors {
|
||
10 years ago
|
if errs.Len() == 0 {
|
||
|
return errs
|
||
10 years ago
|
}
|
||
|
|
||
|
data["HasError"] = true
|
||
5 years ago
|
// If the field with name errs[0].FieldNames[0] is not found in form
|
||
|
// somehow, some code later on will panic on Data["ErrorMsg"].(string).
|
||
|
// So initialize it to some default.
|
||
|
data["ErrorMsg"] = l.Tr("form.unknown_error")
|
||
10 years ago
|
AssignForm(f, data)
|
||
|
|
||
|
typ := reflect.TypeOf(f)
|
||
|
val := reflect.ValueOf(f)
|
||
|
|
||
|
if typ.Kind() == reflect.Ptr {
|
||
|
typ = typ.Elem()
|
||
|
val = val.Elem()
|
||
|
}
|
||
|
|
||
5 years ago
|
if field, ok := typ.FieldByName(errs[0].FieldNames[0]); ok {
|
||
10 years ago
|
fieldName := field.Tag.Get("form")
|
||
5 years ago
|
if fieldName != "-" {
|
||
8 years ago
|
data["Err_"+field.Name] = true
|
||
9 years ago
|
|
||
|
trName := field.Tag.Get("locale")
|
||
|
if len(trName) == 0 {
|
||
|
trName = l.Tr("form." + field.Name)
|
||
|
} else {
|
||
|
trName = l.Tr(trName)
|
||
|
}
|
||
|
|
||
10 years ago
|
switch errs[0].Classification {
|
||
10 years ago
|
case binding.ERR_REQUIRED:
|
||
10 years ago
|
data["ErrorMsg"] = trName + l.Tr("form.require_error")
|
||
10 years ago
|
case binding.ERR_ALPHA_DASH:
|
||
10 years ago
|
data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_error")
|
||
10 years ago
|
case binding.ERR_ALPHA_DASH_DOT:
|
||
10 years ago
|
data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error")
|
||
8 years ago
|
case validation.ErrGitRefName:
|
||
|
data["ErrorMsg"] = trName + l.Tr("form.git_ref_name_error")
|
||
9 years ago
|
case binding.ERR_SIZE:
|
||
|
data["ErrorMsg"] = trName + l.Tr("form.size_error", GetSize(field))
|
||
10 years ago
|
case binding.ERR_MIN_SIZE:
|
||
10 years ago
|
data["ErrorMsg"] = trName + l.Tr("form.min_size_error", GetMinSize(field))
|
||
10 years ago
|
case binding.ERR_MAX_SIZE:
|
||
10 years ago
|
data["ErrorMsg"] = trName + l.Tr("form.max_size_error", GetMaxSize(field))
|
||
10 years ago
|
case binding.ERR_EMAIL:
|
||
10 years ago
|
data["ErrorMsg"] = trName + l.Tr("form.email_error")
|
||
10 years ago
|
case binding.ERR_URL:
|
||
10 years ago
|
data["ErrorMsg"] = trName + l.Tr("form.url_error")
|
||
9 years ago
|
case binding.ERR_INCLUDE:
|
||
|
data["ErrorMsg"] = trName + l.Tr("form.include_error", GetInclude(field))
|
||
5 years ago
|
case validation.ErrGlobPattern:
|
||
|
data["ErrorMsg"] = trName + l.Tr("form.glob_pattern_error", errs[0].Message)
|
||
10 years ago
|
default:
|
||
10 years ago
|
data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + errs[0].Classification
|
||
10 years ago
|
}
|
||
10 years ago
|
return errs
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
return errs
|
||
11 years ago
|
}
|