mirror of https://github.com/gogits/gogs.git
Unknwon
8 years ago
28 changed files with 865 additions and 874 deletions
@ -1,55 +0,0 @@
|
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package auth |
||||
|
||||
import ( |
||||
"github.com/go-macaron/binding" |
||||
"gopkg.in/macaron.v1" |
||||
) |
||||
|
||||
// ________ .__ __ .__
|
||||
// \_____ \_______ _________ ____ |__|____________ _/ |_|__| ____ ____
|
||||
// / | \_ __ \/ ___\__ \ / \| \___ /\__ \\ __\ |/ _ \ / \
|
||||
// / | \ | \/ /_/ > __ \| | \ |/ / / __ \| | | ( <_> ) | \
|
||||
// \_______ /__| \___ (____ /___| /__/_____ \(____ /__| |__|\____/|___| /
|
||||
// \/ /_____/ \/ \/ \/ \/ \/
|
||||
|
||||
type CreateOrgForm struct { |
||||
OrgName string `binding:"Required;AlphaDashDot;MaxSize(35)" locale:"org.org_name_holder"` |
||||
} |
||||
|
||||
func (f *CreateOrgForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { |
||||
return validate(errs, ctx.Data, f, ctx.Locale) |
||||
} |
||||
|
||||
type UpdateOrgSettingForm struct { |
||||
Name string `binding:"Required;AlphaDashDot;MaxSize(35)" locale:"org.org_name_holder"` |
||||
FullName string `binding:"MaxSize(100)"` |
||||
Description string `binding:"MaxSize(255)"` |
||||
Website string `binding:"Url;MaxSize(100)"` |
||||
Location string `binding:"MaxSize(50)"` |
||||
MaxRepoCreation int |
||||
} |
||||
|
||||
func (f *UpdateOrgSettingForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { |
||||
return validate(errs, ctx.Data, f, ctx.Locale) |
||||
} |
||||
|
||||
// ___________
|
||||
// \__ ___/___ _____ _____
|
||||
// | |_/ __ \\__ \ / \
|
||||
// | |\ ___/ / __ \| Y Y \
|
||||
// |____| \___ >____ /__|_| /
|
||||
// \/ \/ \/
|
||||
|
||||
type CreateTeamForm struct { |
||||
TeamName string `binding:"Required;AlphaDashDot;MaxSize(30)"` |
||||
Description string `binding:"MaxSize(255)"` |
||||
Permission string |
||||
} |
||||
|
||||
func (f *CreateTeamForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { |
||||
return validate(errs, ctx.Data, f, ctx.Locale) |
||||
} |
@ -0,0 +1,135 @@
|
||||
// Copyright 2017 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package form |
||||
|
||||
import ( |
||||
"reflect" |
||||
"strings" |
||||
|
||||
"github.com/Unknwon/com" |
||||
"github.com/go-macaron/binding" |
||||
"gopkg.in/macaron.v1" |
||||
) |
||||
|
||||
func init() { |
||||
binding.SetNameMapper(com.ToSnakeCase) |
||||
} |
||||
|
||||
type Form interface { |
||||
binding.Validator |
||||
} |
||||
|
||||
// Assign assign form values back to the template data.
|
||||
func Assign(form interface{}, data map[string]interface{}) { |
||||
typ := reflect.TypeOf(form) |
||||
val := reflect.ValueOf(form) |
||||
|
||||
if typ.Kind() == reflect.Ptr { |
||||
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 |
||||
} else if len(fieldName) == 0 { |
||||
fieldName = com.ToSnakeCase(field.Name) |
||||
} |
||||
|
||||
data[fieldName] = val.Field(i).Interface() |
||||
} |
||||
} |
||||
|
||||
func getRuleBody(field reflect.StructField, prefix string) string { |
||||
for _, rule := range strings.Split(field.Tag.Get("binding"), ";") { |
||||
if strings.HasPrefix(rule, prefix) { |
||||
return rule[len(prefix) : len(rule)-1] |
||||
} |
||||
} |
||||
return "" |
||||
} |
||||
|
||||
func getSize(field reflect.StructField) string { |
||||
return getRuleBody(field, "Size(") |
||||
} |
||||
|
||||
func getMinSize(field reflect.StructField) string { |
||||
return getRuleBody(field, "MinSize(") |
||||
} |
||||
|
||||
func getMaxSize(field reflect.StructField) string { |
||||
return getRuleBody(field, "MaxSize(") |
||||
} |
||||
|
||||
func getInclude(field reflect.StructField) string { |
||||
return getRuleBody(field, "Include(") |
||||
} |
||||
|
||||
func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaron.Locale) binding.Errors { |
||||
if errs.Len() == 0 { |
||||
return errs |
||||
} |
||||
|
||||
data["HasError"] = true |
||||
Assign(f, data) |
||||
|
||||
typ := reflect.TypeOf(f) |
||||
val := reflect.ValueOf(f) |
||||
|
||||
if typ.Kind() == reflect.Ptr { |
||||
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 |
||||
} |
||||
|
||||
if errs[0].FieldNames[0] == field.Name { |
||||
data["Err_"+field.Name] = true |
||||
|
||||
trName := field.Tag.Get("locale") |
||||
if len(trName) == 0 { |
||||
trName = l.Tr("form." + field.Name) |
||||
} else { |
||||
trName = l.Tr(trName) |
||||
} |
||||
|
||||
switch errs[0].Classification { |
||||
case binding.ERR_REQUIRED: |
||||
data["ErrorMsg"] = trName + l.Tr("form.require_error") |
||||
case binding.ERR_ALPHA_DASH: |
||||
data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_error") |
||||
case binding.ERR_ALPHA_DASH_DOT: |
||||
data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error") |
||||
case binding.ERR_SIZE: |
||||
data["ErrorMsg"] = trName + l.Tr("form.size_error", getSize(field)) |
||||
case binding.ERR_MIN_SIZE: |
||||
data["ErrorMsg"] = trName + l.Tr("form.min_size_error", getMinSize(field)) |
||||
case binding.ERR_MAX_SIZE: |
||||
data["ErrorMsg"] = trName + l.Tr("form.max_size_error", getMaxSize(field)) |
||||
case binding.ERR_EMAIL: |
||||
data["ErrorMsg"] = trName + l.Tr("form.email_error") |
||||
case binding.ERR_URL: |
||||
data["ErrorMsg"] = trName + l.Tr("form.url_error") |
||||
case binding.ERR_INCLUDE: |
||||
data["ErrorMsg"] = trName + l.Tr("form.include_error", getInclude(field)) |
||||
default: |
||||
data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + errs[0].Classification |
||||
} |
||||
return errs |
||||
} |
||||
} |
||||
return errs |
||||
} |
@ -0,0 +1,41 @@
|
||||
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package form |
||||
|
||||
import ( |
||||
"github.com/go-macaron/binding" |
||||
"gopkg.in/macaron.v1" |
||||
) |
||||
|
||||
type CreateOrg struct { |
||||
OrgName string `binding:"Required;AlphaDashDot;MaxSize(35)" locale:"org.org_name_holder"` |
||||
} |
||||
|
||||
func (f *CreateOrg) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { |
||||
return validate(errs, ctx.Data, f, ctx.Locale) |
||||
} |
||||
|
||||
type UpdateOrgSetting struct { |
||||
Name string `binding:"Required;AlphaDashDot;MaxSize(35)" locale:"org.org_name_holder"` |
||||
FullName string `binding:"MaxSize(100)"` |
||||
Description string `binding:"MaxSize(255)"` |
||||
Website string `binding:"Url;MaxSize(100)"` |
||||
Location string `binding:"MaxSize(50)"` |
||||
MaxRepoCreation int |
||||
} |
||||
|
||||
func (f *UpdateOrgSetting) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { |
||||
return validate(errs, ctx.Data, f, ctx.Locale) |
||||
} |
||||
|
||||
type CreateTeam struct { |
||||
TeamName string `binding:"Required;AlphaDashDot;MaxSize(30)"` |
||||
Description string `binding:"MaxSize(255)"` |
||||
Permission string |
||||
} |
||||
|
||||
func (f *CreateTeam) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { |
||||
return validate(errs, ctx.Data, f, ctx.Locale) |
||||
} |
Loading…
Reference in new issue