This commit is contained in:
Unknwon 2014-08-01 06:12:14 -04:00
parent 17d2675dd2
commit 669552e255
3 changed files with 19 additions and 11 deletions

View File

@ -63,9 +63,9 @@ func validateApiReq(errs *binding.Errors, data map[string]interface{}, f interfa
case binding.BindingAlphaDashDotError: case binding.BindingAlphaDashDotError:
data["ErrorMsg"] = fieldName + " must be valid alpha or numeric or dash(-_) or dot characters" data["ErrorMsg"] = fieldName + " must be valid alpha or numeric or dash(-_) or dot characters"
case binding.BindingMinSizeError: case binding.BindingMinSizeError:
data["ErrorMsg"] = fieldName + " must contain at least " + auth.GetMinMaxSize(field) + " characters" data["ErrorMsg"] = fieldName + " must contain at least " + auth.GetMinSize(field) + " characters"
case binding.BindingMaxSizeError: case binding.BindingMaxSizeError:
data["ErrorMsg"] = fieldName + " must contain at most " + auth.GetMinMaxSize(field) + " characters" data["ErrorMsg"] = fieldName + " must contain at most " + auth.GetMaxSize(field) + " characters"
case binding.BindingEmailError: case binding.BindingEmailError:
data["ErrorMsg"] = fieldName + " is not a valid e-mail address" data["ErrorMsg"] = fieldName + " is not a valid e-mail address"
case binding.BindingUrlError: case binding.BindingUrlError:

View File

@ -92,15 +92,23 @@ func AssignForm(form interface{}, data map[string]interface{}) {
} }
} }
func GetMinMaxSize(field reflect.StructField) string { func getSize(field reflect.StructField, prefix string) string {
for _, rule := range strings.Split(field.Tag.Get("binding"), ";") { for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") { if strings.HasPrefix(rule, prefix) {
return rule[8 : len(rule)-1] return rule[8 : len(rule)-1]
} }
} }
return "" return ""
} }
func GetMinSize(field reflect.StructField) string {
return getSize(field, "MinSize(")
}
func GetMaxSize(field reflect.StructField) string {
return getSize(field, "MaxSize(")
}
func validate(errs *binding.Errors, data map[string]interface{}, f interface{}, l i18n.Locale) { func validate(errs *binding.Errors, data map[string]interface{}, f interface{}, l i18n.Locale) {
if errs.Count() == 0 { if errs.Count() == 0 {
return return
@ -142,9 +150,9 @@ func validate(errs *binding.Errors, data map[string]interface{}, f interface{},
case binding.BindingAlphaDashDotError: case binding.BindingAlphaDashDotError:
data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error") data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error")
case binding.BindingMinSizeError: case binding.BindingMinSizeError:
data["ErrorMsg"] = trName + l.Tr("form.min_size_error", GetMinMaxSize(field)) data["ErrorMsg"] = trName + l.Tr("form.min_size_error", GetMinSize(field))
case binding.BindingMaxSizeError: case binding.BindingMaxSizeError:
data["ErrorMsg"] = trName + l.Tr("form.max_size_error", GetMinMaxSize(field)) data["ErrorMsg"] = trName + l.Tr("form.max_size_error", GetMaxSize(field))
case binding.BindingEmailError: case binding.BindingEmailError:
data["ErrorMsg"] = trName + l.Tr("form.email_error") data["ErrorMsg"] = trName + l.Tr("form.email_error")
case binding.BindingUrlError: case binding.BindingUrlError:

View File

@ -24,7 +24,7 @@ type InstallForm struct {
Domain string `form:"domain"` Domain string `form:"domain"`
AppUrl string `form:"app_url"` AppUrl string `form:"app_url"`
AdminName string `form:"admin_name" binding:"Required;AlphaDashDot;MaxSize(30)"` AdminName string `form:"admin_name" binding:"Required;AlphaDashDot;MaxSize(30)"`
AdminPasswd string `form:"admin_pwd" binding:"Required;MinSize(6);MaxSize(30)"` AdminPasswd string `form:"admin_pwd" binding:"Required;MinSize(6);MaxSize(255)"`
AdminEmail string `form:"admin_email" binding:"Required;Email;MaxSize(50)"` AdminEmail string `form:"admin_email" binding:"Required;Email;MaxSize(50)"`
SmtpHost string `form:"smtp_host"` SmtpHost string `form:"smtp_host"`
SmtpEmail string `form:"mailer_user"` SmtpEmail string `form:"mailer_user"`
@ -47,7 +47,7 @@ func (f *InstallForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i18
type RegisterForm struct { type RegisterForm struct {
UserName string `form:"uname" binding:"Required;AlphaDashDot;MaxSize(35)"` UserName string `form:"uname" binding:"Required;AlphaDashDot;MaxSize(35)"`
Email string `form:"email" binding:"Required;Email;MaxSize(50)"` Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
Password string `form:"password" binding:"Required;MinSize(6);MaxSize(30)"` Password string `form:"password" binding:"Required;MinSize(6);MaxSize(255)"`
Retype string `form:"retype"` Retype string `form:"retype"`
LoginType string `form:"logintype"` LoginType string `form:"logintype"`
LoginName string `form:"loginname"` LoginName string `form:"loginname"`
@ -59,7 +59,7 @@ func (f *RegisterForm) Validate(ctx *macaron.Context, errs *binding.Errors, l i1
type SignInForm struct { type SignInForm struct {
UserName string `form:"uname" binding:"Required;MaxSize(35)"` UserName string `form:"uname" binding:"Required;MaxSize(35)"`
Password string `form:"password" binding:"Required;MinSize(6);MaxSize(30)"` Password string `form:"password" binding:"Required;MinSize(6);MaxSize(255)"`
Remember bool `form:"remember"` Remember bool `form:"remember"`
} }
@ -88,8 +88,8 @@ func (f *UpdateProfileForm) Validate(ctx *macaron.Context, errs *binding.Errors,
} }
type ChangePasswordForm struct { type ChangePasswordForm struct {
OldPassword string `form:"old_password" binding:"Required;MinSize(6);MaxSize(30)"` OldPassword string `form:"old_password" binding:"Required;MinSize(6);MaxSize(255)"`
Password string `form:"password" binding:"Required;MinSize(6);MaxSize(30)"` Password string `form:"password" binding:"Required;MinSize(6);MaxSize(255)"`
Retype string `form:"retype"` Retype string `form:"retype"`
} }