From 2a1dc0085b9f7236082f3885fa594ec12d5b0211 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 1 May 2014 07:35:21 -0400 Subject: [PATCH] fix #91 --- modules/auth/auth.go | 4 +++- modules/base/base.go | 1 + modules/middleware/binding.go | 12 +++++++++--- templates/base/navbar.tmpl | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/modules/auth/auth.go b/modules/auth/auth.go index e493faefe72b..85df42d8c407 100644 --- a/modules/auth/auth.go +++ b/modules/auth/auth.go @@ -21,7 +21,7 @@ type Form interface { } type RegisterForm struct { - UserName string `form:"username" binding:"Required;AlphaDash;MaxSize(30)"` + UserName string `form:"username" binding:"Required;AlphaDashDot;MaxSize(30)"` Email string `form:"email" binding:"Required;Email;MaxSize(50)"` Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"` RetypePasswd string `form:"retypepasswd"` @@ -123,6 +123,8 @@ func validate(errors *base.BindingErrors, data base.TmplData, form Form) { data["ErrorMsg"] = form.Name(field.Name) + " cannot be empty" case base.BindingAlphaDashError: data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters" + case base.BindingAlphaDashDotError: + data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) or dot characters" case base.BindingMinSizeError: data["ErrorMsg"] = form.Name(field.Name) + " must contain at least " + getMinMaxSize(field) + " characters" case base.BindingMaxSizeError: diff --git a/modules/base/base.go b/modules/base/base.go index 3e80a436e4c2..e6befb83fd20 100644 --- a/modules/base/base.go +++ b/modules/base/base.go @@ -45,6 +45,7 @@ func (this *BindingErrors) Combine(other BindingErrors) { const ( BindingRequireError string = "Required" BindingAlphaDashError string = "AlphaDash" + BindingAlphaDashDotError string = "AlphaDashDot" BindingMinSizeError string = "MinSize" BindingMaxSizeError string = "MaxSize" BindingEmailError string = "Email" diff --git a/modules/middleware/binding.go b/modules/middleware/binding.go index cde9ae9cc0a1..bff34ddde030 100644 --- a/modules/middleware/binding.go +++ b/modules/middleware/binding.go @@ -180,9 +180,10 @@ func Validate(obj interface{}) martini.Handler { } var ( - alphaDashPattern = regexp.MustCompile("[^\\d\\w-_]") - emailPattern = regexp.MustCompile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z0-9](?:[\\w-]*[\\w])?") - urlPattern = regexp.MustCompile(`(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?`) + alphaDashPattern = regexp.MustCompile("[^\\d\\w-_]") + alphaDashDotPattern = regexp.MustCompile("[^\\d\\w-_\\.]") + emailPattern = regexp.MustCompile("[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[a-zA-Z0-9](?:[\\w-]*[\\w])?") + urlPattern = regexp.MustCompile(`(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?`) ) func validateStruct(errors *base.BindingErrors, obj interface{}) { @@ -227,6 +228,11 @@ func validateStruct(errors *base.BindingErrors, obj interface{}) { errors.Fields[field.Name] = base.BindingAlphaDashError break } + case rule == "AlphaDashDot": + if alphaDashDotPattern.MatchString(fmt.Sprintf("%v", fieldValue)) { + errors.Fields[field.Name] = base.BindingAlphaDashDotError + break + } case strings.HasPrefix(rule, "MinSize("): min, err := strconv.Atoi(rule[8 : len(rule)-1]) if err != nil { diff --git a/templates/base/navbar.tmpl b/templates/base/navbar.tmpl index da6865b8e8d1..80085d974a5c 100644 --- a/templates/base/navbar.tmpl +++ b/templates/base/navbar.tmpl @@ -3,7 +3,7 @@