You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

form.go 4.1 kB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package auth
  5. import (
  6. "net/http"
  7. "reflect"
  8. "strings"
  9. "github.com/codegangsta/martini"
  10. "github.com/gogits/binding"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/utils/log"
  13. )
  14. type Form interface {
  15. Name(field string) string
  16. }
  17. type RegisterForm struct {
  18. UserName string `form:"username" binding:"Required;AlphaDash;MinSize(5);MaxSize(30)"`
  19. Email string `form:"email" binding:"Required;Email;MaxSize(50)"`
  20. Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"`
  21. RetypePasswd string `form:"retypepasswd"`
  22. }
  23. func (f *RegisterForm) Name(field string) string {
  24. names := map[string]string{
  25. "UserName": "Username",
  26. "Email": "E-mail address",
  27. "Password": "Password",
  28. "RetypePasswd": "Re-type password",
  29. }
  30. return names[field]
  31. }
  32. func (f *RegisterForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  33. if req.Method == "GET" || errors.Count() == 0 {
  34. return
  35. }
  36. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  37. data["HasError"] = true
  38. AssignForm(f, data)
  39. if len(errors.Overall) > 0 {
  40. for _, err := range errors.Overall {
  41. log.Error("RegisterForm.Validate: %v", err)
  42. }
  43. return
  44. }
  45. validate(errors, data, f)
  46. }
  47. type LogInForm struct {
  48. UserName string `form:"username" binding:"Required;AlphaDash;MinSize(5);MaxSize(30)"`
  49. Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"`
  50. }
  51. func (f *LogInForm) Name(field string) string {
  52. names := map[string]string{
  53. "UserName": "Username",
  54. "Password": "Password",
  55. }
  56. return names[field]
  57. }
  58. func (f *LogInForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  59. if req.Method == "GET" || errors.Count() == 0 {
  60. return
  61. }
  62. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  63. data["HasError"] = true
  64. AssignForm(f, data)
  65. if len(errors.Overall) > 0 {
  66. for _, err := range errors.Overall {
  67. log.Error("LogInForm.Validate: %v", err)
  68. }
  69. return
  70. }
  71. validate(errors, data, f)
  72. }
  73. func getMinMaxSize(field reflect.StructField) string {
  74. for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
  75. if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
  76. return rule[8 : len(rule)-1]
  77. }
  78. }
  79. return ""
  80. }
  81. func validate(errors *binding.Errors, data base.TmplData, form Form) {
  82. typ := reflect.TypeOf(form)
  83. val := reflect.ValueOf(form)
  84. if typ.Kind() == reflect.Ptr {
  85. typ = typ.Elem()
  86. val = val.Elem()
  87. }
  88. for i := 0; i < typ.NumField(); i++ {
  89. field := typ.Field(i)
  90. fieldName := field.Tag.Get("form")
  91. // Allow ignored fields in the struct
  92. if fieldName == "-" {
  93. continue
  94. }
  95. if err, ok := errors.Fields[field.Name]; ok {
  96. data["Err_"+field.Name] = true
  97. switch err {
  98. case binding.RequireError:
  99. data["ErrorMsg"] = form.Name(field.Name) + " cannot be empty"
  100. case binding.AlphaDashError:
  101. data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters"
  102. case binding.MinSizeError:
  103. data["ErrorMsg"] = form.Name(field.Name) + " must contain at least " + getMinMaxSize(field) + " characters"
  104. case binding.MaxSizeError:
  105. data["ErrorMsg"] = form.Name(field.Name) + " must contain at most " + getMinMaxSize(field) + " characters"
  106. case binding.EmailError:
  107. data["ErrorMsg"] = form.Name(field.Name) + " is not valid"
  108. default:
  109. data["ErrorMsg"] = "Unknown error: " + err
  110. }
  111. return
  112. }
  113. }
  114. }
  115. // AssignForm assign form values back to the template data.
  116. func AssignForm(form interface{}, data base.TmplData) {
  117. typ := reflect.TypeOf(form)
  118. val := reflect.ValueOf(form)
  119. if typ.Kind() == reflect.Ptr {
  120. typ = typ.Elem()
  121. val = val.Elem()
  122. }
  123. for i := 0; i < typ.NumField(); i++ {
  124. field := typ.Field(i)
  125. fieldName := field.Tag.Get("form")
  126. // Allow ignored fields in the struct
  127. if fieldName == "-" {
  128. continue
  129. }
  130. data[fieldName] = val.Field(i).Interface()
  131. }
  132. }