|
|
@ -7,6 +7,7 @@ package auth |
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"net/http" |
|
|
|
"net/http" |
|
|
|
"reflect" |
|
|
|
"reflect" |
|
|
|
|
|
|
|
"strings" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/codegangsta/martini" |
|
|
|
"github.com/codegangsta/martini" |
|
|
|
|
|
|
|
|
|
|
@ -17,74 +18,76 @@ import ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type RegisterForm struct { |
|
|
|
type RegisterForm struct { |
|
|
|
UserName string `form:"username" binding:"Required;AlphaDash;MinSize(5);MaxSize(30)"` |
|
|
|
Username string `form:"username" binding:"Required;AlphaDash;MinSize(5);MaxSize(30)"` |
|
|
|
Email string `form:"email" binding:"Required;Email;MaxSize(50)"` |
|
|
|
Email string `form:"email" binding:"Required;Email;MaxSize(50)"` |
|
|
|
Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"` |
|
|
|
Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"` |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (r *RegisterForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { |
|
|
|
func getMinMaxSize(field reflect.StructField) string { |
|
|
|
if req.Method == "GET" || errors.Count() == 0 { |
|
|
|
for _, rule := range strings.Split(field.Tag.Get("binding"), ";") { |
|
|
|
return |
|
|
|
if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") { |
|
|
|
|
|
|
|
return rule[8 : len(rule)-1] |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return "" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) |
|
|
|
func validate(errors *binding.Errors, data base.TmplData, form interface{}) { |
|
|
|
data["HasError"] = true |
|
|
|
typ := reflect.TypeOf(form) |
|
|
|
AssignForm(r, data) |
|
|
|
val := reflect.ValueOf(form) |
|
|
|
|
|
|
|
|
|
|
|
if len(errors.Overall) > 0 { |
|
|
|
if typ.Kind() == reflect.Ptr { |
|
|
|
for _, err := range errors.Overall { |
|
|
|
typ = typ.Elem() |
|
|
|
log.Error("RegisterForm.Validate: %v", err) |
|
|
|
val = val.Elem() |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
|
|
|
|
|
|
|
|
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["UserName"]; ok { |
|
|
|
if err, ok := errors.Fields[field.Name]; ok { |
|
|
|
data["Err_Username"] = true |
|
|
|
data["Err_"+field.Name] = true |
|
|
|
switch err { |
|
|
|
switch err { |
|
|
|
case binding.RequireError: |
|
|
|
case binding.RequireError: |
|
|
|
data["ErrorMsg"] = "Username cannot be empty" |
|
|
|
data["ErrorMsg"] = field.Name + " cannot be empty" |
|
|
|
case binding.AlphaDashError: |
|
|
|
case binding.AlphaDashError: |
|
|
|
data["ErrorMsg"] = "Username must be valid alpha or numeric or dash(-_) characters" |
|
|
|
data["ErrorMsg"] = field.Name + " must be valid alpha or numeric or dash(-_) characters" |
|
|
|
case binding.MinSizeError: |
|
|
|
case binding.MinSizeError: |
|
|
|
data["ErrorMsg"] = "Username at least has 5 characters" |
|
|
|
data["ErrorMsg"] = field.Name + " must contain at least has " + getMinMaxSize(field) + " characters" |
|
|
|
case binding.MaxSizeError: |
|
|
|
case binding.MaxSizeError: |
|
|
|
data["ErrorMsg"] = "Username at most has 30 characters" |
|
|
|
data["ErrorMsg"] = field.Name + " must contain at most has " + getMinMaxSize(field) + " characters" |
|
|
|
|
|
|
|
case binding.EmailError: |
|
|
|
|
|
|
|
data["ErrorMsg"] = field.Name + " is not valid" |
|
|
|
default: |
|
|
|
default: |
|
|
|
data["ErrorMsg"] = "Unknown error: " + err |
|
|
|
data["ErrorMsg"] = "Unknown error: " + err |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if err, ok := errors.Fields["Email"]; ok { |
|
|
|
|
|
|
|
data["Err_Email"] = true |
|
|
|
|
|
|
|
switch err { |
|
|
|
|
|
|
|
case binding.RequireError: |
|
|
|
|
|
|
|
data["ErrorMsg"] = "E-mail address cannot be empty" |
|
|
|
|
|
|
|
case binding.EmailError: |
|
|
|
|
|
|
|
data["ErrorMsg"] = "E-mail address is not valid" |
|
|
|
|
|
|
|
case binding.MaxSizeError: |
|
|
|
|
|
|
|
data["ErrorMsg"] = "E-mail address at most has 50 characters" |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
data["ErrorMsg"] = "Unknown error: " + err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (r *RegisterForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { |
|
|
|
|
|
|
|
if req.Method == "GET" || errors.Count() == 0 { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if err, ok := errors.Fields["Password"]; ok { |
|
|
|
data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) |
|
|
|
data["Err_Passwd"] = true |
|
|
|
data["HasError"] = true |
|
|
|
switch err { |
|
|
|
AssignForm(r, data) |
|
|
|
case binding.RequireError: |
|
|
|
|
|
|
|
data["ErrorMsg"] = "Password cannot be empty" |
|
|
|
if len(errors.Overall) > 0 { |
|
|
|
case binding.MinSizeError: |
|
|
|
for _, err := range errors.Overall { |
|
|
|
data["ErrorMsg"] = "Password at least has 6 characters" |
|
|
|
log.Error("RegisterForm.Validate: %v", err) |
|
|
|
case binding.MaxSizeError: |
|
|
|
|
|
|
|
data["ErrorMsg"] = "Password at most has 30 characters" |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
data["ErrorMsg"] = "Unknown error: " + err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
validate(errors, data, r) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AssignForm assign form values back to the template data.
|
|
|
|
// AssignForm assign form values back to the template data.
|
|
|
|