gitea/modules/auth/form.go

115 lines
2.9 KiB
Go
Raw Normal View History

2014-03-06 00:21:44 -07:00
// 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 (
"net/http"
"reflect"
2014-03-06 00:59:44 -07:00
"strings"
2014-03-06 00:21:44 -07:00
"github.com/codegangsta/martini"
"github.com/gogits/binding"
"github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/utils/log"
)
type RegisterForm struct {
2014-03-06 00:59:44 -07:00
Username string `form:"username" binding:"Required;AlphaDash;MinSize(5);MaxSize(30)"`
2014-03-06 00:21:44 -07:00
Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"`
}
2014-03-06 00:59:44 -07:00
func getMinMaxSize(field reflect.StructField) string {
for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
return rule[8 : len(rule)-1]
}
}
return ""
}
func validate(errors *binding.Errors, data base.TmplData, form 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
}
if err, ok := errors.Fields[field.Name]; ok {
data["Err_"+field.Name] = true
switch err {
case binding.RequireError:
data["ErrorMsg"] = field.Name + " cannot be empty"
case binding.AlphaDashError:
data["ErrorMsg"] = field.Name + " must be valid alpha or numeric or dash(-_) characters"
case binding.MinSizeError:
data["ErrorMsg"] = field.Name + " must contain at least has " + getMinMaxSize(field) + " characters"
case binding.MaxSizeError:
data["ErrorMsg"] = field.Name + " must contain at most has " + getMinMaxSize(field) + " characters"
case binding.EmailError:
data["ErrorMsg"] = field.Name + " is not valid"
default:
data["ErrorMsg"] = "Unknown error: " + err
}
return
}
}
}
2014-03-06 00:21:44 -07:00
func (r *RegisterForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
if req.Method == "GET" || errors.Count() == 0 {
return
}
data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
data["HasError"] = true
AssignForm(r, data)
if len(errors.Overall) > 0 {
for _, err := range errors.Overall {
log.Error("RegisterForm.Validate: %v", err)
}
return
}
2014-03-06 00:59:44 -07:00
validate(errors, data, r)
2014-03-06 00:21:44 -07:00
}
// AssignForm assign form values back to the template data.
func AssignForm(form interface{}, data base.TmplData) {
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
}
data[fieldName] = val.Field(i).Interface()
}
}