|
@@ -28,7 +28,7 @@ type RegisterForm struct {
|
|
RetypePasswd string `form:"retypepasswd"`
|
|
RetypePasswd string `form:"retypepasswd"`
|
|
}
|
|
}
|
|
|
|
|
|
-func (r *RegisterForm) Name(field string) string {
|
|
|
|
|
|
+func (f *RegisterForm) Name(field string) string {
|
|
names := map[string]string{
|
|
names := map[string]string{
|
|
"UserName": "Username",
|
|
"UserName": "Username",
|
|
"Email": "E-mail address",
|
|
"Email": "E-mail address",
|
|
@@ -38,6 +38,57 @@ func (r *RegisterForm) Name(field string) string {
|
|
return names[field]
|
|
return names[field]
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (f *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(f, data)
|
|
|
|
+
|
|
|
|
+ if len(errors.Overall) > 0 {
|
|
|
|
+ for _, err := range errors.Overall {
|
|
|
|
+ log.Error("RegisterForm.Validate: %v", err)
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ validate(errors, data, f)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type LogInForm struct {
|
|
|
|
+ UserName string `form:"username" binding:"Required;AlphaDash;MinSize(5);MaxSize(30)"`
|
|
|
|
+ Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"`
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (f *LogInForm) Name(field string) string {
|
|
|
|
+ names := map[string]string{
|
|
|
|
+ "UserName": "Username",
|
|
|
|
+ "Password": "Password",
|
|
|
|
+ }
|
|
|
|
+ return names[field]
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (f *LogInForm) 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(f, data)
|
|
|
|
+
|
|
|
|
+ if len(errors.Overall) > 0 {
|
|
|
|
+ for _, err := range errors.Overall {
|
|
|
|
+ log.Error("LogInForm.Validate: %v", err)
|
|
|
|
+ }
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ validate(errors, data, f)
|
|
|
|
+}
|
|
|
|
+
|
|
func getMinMaxSize(field reflect.StructField) string {
|
|
func getMinMaxSize(field reflect.StructField) 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, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
|
|
@@ -86,25 +137,6 @@ func validate(errors *binding.Errors, data base.TmplData, form Form) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-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
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- validate(errors, data, r)
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
// AssignForm assign form values back to the template data.
|
|
// AssignForm assign form values back to the template data.
|
|
func AssignForm(form interface{}, data base.TmplData) {
|
|
func AssignForm(form interface{}, data base.TmplData) {
|
|
typ := reflect.TypeOf(form)
|
|
typ := reflect.TypeOf(form)
|