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 3.3 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 (r *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 getMinMaxSize(field reflect.StructField) string {
  33. for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
  34. if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
  35. return rule[8 : len(rule)-1]
  36. }
  37. }
  38. return ""
  39. }
  40. func validate(errors *binding.Errors, data base.TmplData, form Form) {
  41. typ := reflect.TypeOf(form)
  42. val := reflect.ValueOf(form)
  43. if typ.Kind() == reflect.Ptr {
  44. typ = typ.Elem()
  45. val = val.Elem()
  46. }
  47. for i := 0; i < typ.NumField(); i++ {
  48. field := typ.Field(i)
  49. fieldName := field.Tag.Get("form")
  50. // Allow ignored fields in the struct
  51. if fieldName == "-" {
  52. continue
  53. }
  54. if err, ok := errors.Fields[field.Name]; ok {
  55. data["Err_"+field.Name] = true
  56. switch err {
  57. case binding.RequireError:
  58. data["ErrorMsg"] = form.Name(field.Name) + " cannot be empty"
  59. case binding.AlphaDashError:
  60. data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters"
  61. case binding.MinSizeError:
  62. data["ErrorMsg"] = form.Name(field.Name) + " must contain at least " + getMinMaxSize(field) + " characters"
  63. case binding.MaxSizeError:
  64. data["ErrorMsg"] = form.Name(field.Name) + " must contain at most " + getMinMaxSize(field) + " characters"
  65. case binding.EmailError:
  66. data["ErrorMsg"] = form.Name(field.Name) + " is not valid"
  67. default:
  68. data["ErrorMsg"] = "Unknown error: " + err
  69. }
  70. return
  71. }
  72. }
  73. }
  74. func (r *RegisterForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) {
  75. if req.Method == "GET" || errors.Count() == 0 {
  76. return
  77. }
  78. data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData)
  79. data["HasError"] = true
  80. AssignForm(r, data)
  81. if len(errors.Overall) > 0 {
  82. for _, err := range errors.Overall {
  83. log.Error("RegisterForm.Validate: %v", err)
  84. }
  85. return
  86. }
  87. validate(errors, data, r)
  88. }
  89. // AssignForm assign form values back to the template data.
  90. func AssignForm(form interface{}, data base.TmplData) {
  91. typ := reflect.TypeOf(form)
  92. val := reflect.ValueOf(form)
  93. if typ.Kind() == reflect.Ptr {
  94. typ = typ.Elem()
  95. val = val.Elem()
  96. }
  97. for i := 0; i < typ.NumField(); i++ {
  98. field := typ.Field(i)
  99. fieldName := field.Tag.Get("form")
  100. // Allow ignored fields in the struct
  101. if fieldName == "-" {
  102. continue
  103. }
  104. data[fieldName] = val.Field(i).Interface()
  105. }
  106. }