MCPcopy Create free account
hub / github.com/gogs/gogs / validate

Function validate

internal/form/form.go:89–149  ·  view source on GitHub ↗
(errs binding.Errors, data map[string]any, f Form, l macaron.Locale)

Source from the content-addressed store, hash-verified

87}
88
89func validate(errs binding.Errors, data map[string]any, f Form, l macaron.Locale) binding.Errors {
90 if errs.Len() == 0 {
91 return errs
92 }
93
94 data["HasError"] = true
95 Assign(f, data)
96
97 typ := reflect.TypeOf(f)
98 if typ.Kind() == reflect.Pointer {
99 typ = typ.Elem()
100 }
101
102 for i := 0; i < typ.NumField(); i++ {
103 field := typ.Field(i)
104
105 fieldName := field.Tag.Get("form")
106 // Allow ignored fields in the struct
107 if fieldName == "-" {
108 continue
109 }
110
111 if errs[0].FieldNames[0] == field.Name {
112 data["Err_"+field.Name] = true
113
114 trName := field.Tag.Get("locale")
115 if trName == "" {
116 trName = l.Tr("form." + field.Name)
117 } else {
118 trName = l.Tr(trName)
119 }
120
121 switch errs[0].Classification {
122 case binding.ERR_REQUIRED:
123 data["ErrorMsg"] = trName + l.Tr("form.require_error")
124 case binding.ERR_ALPHA_DASH:
125 data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_error")
126 case binding.ERR_ALPHA_DASH_DOT:
127 data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error")
128 case ErrAlphaDashDotSlash:
129 data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_slash_error")
130 case binding.ERR_SIZE:
131 data["ErrorMsg"] = trName + l.Tr("form.size_error", getSize(field))
132 case binding.ERR_MIN_SIZE:
133 data["ErrorMsg"] = trName + l.Tr("form.min_size_error", getMinSize(field))
134 case binding.ERR_MAX_SIZE:
135 data["ErrorMsg"] = trName + l.Tr("form.max_size_error", getMaxSize(field))
136 case binding.ERR_EMAIL:
137 data["ErrorMsg"] = trName + l.Tr("form.email_error")
138 case binding.ERR_URL:
139 data["ErrorMsg"] = trName + l.Tr("form.url_error")
140 case binding.ERR_INCLUDE:
141 data["ErrorMsg"] = trName + l.Tr("form.include_error", getInclude(field))
142 default:
143 data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + errs[0].Classification
144 }
145 return errs
146 }

Callers 15

ValidateMethod · 0.85
ValidateMethod · 0.85
ValidateMethod · 0.85
ValidateMethod · 0.85
ValidateMethod · 0.85
ValidateMethod · 0.85
ValidateMethod · 0.85
ValidateMethod · 0.85
ValidateMethod · 0.85
ValidateMethod · 0.85
ValidateMethod · 0.85
ValidateMethod · 0.85

Calls 8

AssignFunction · 0.85
getSizeFunction · 0.85
getMinSizeFunction · 0.85
getMaxSizeFunction · 0.85
getIncludeFunction · 0.85
TrMethod · 0.80
LenMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected