summaryrefslogtreecommitdiffstats
path: root/modules/auth/form.go
blob: f918543fe65ddd7ce70edb6b9bbbd5685cad0167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package auth

import (
	"net/http"
	"reflect"
	"strings"

	"github.com/codegangsta/martini"

	"github.com/gogits/binding"

	"github.com/gogits/gogs/modules/base"
	"github.com/gogits/gogs/utils/log"
)

type RegisterForm struct {
	Username string `form:"username" binding:"Required;AlphaDash;MinSize(5);MaxSize(30)"`
	Email    string `form:"email" binding:"Required;Email;MaxSize(50)"`
	Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"`
}

func getMinMaxSize(field reflect.StructField) string {
	for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
		if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") {
			return rule[8 : len(rule)-1]
		}
	}
	return ""
}

func validate(errors *binding.Errors, data base.TmplData, form interface{}) {
	typ := reflect.TypeOf(form)
	val := reflect.ValueOf(form)

	if typ.Kind() == reflect.Ptr {
		typ = typ.Elem()
		val = val.Elem()
	}

	for i := 0; i < typ.NumField(); i++ {
		field := typ.Field(i)

		fieldName := field.Tag.Get("form")
		// Allow ignored fields in the struct
		if fieldName == "-" {
			continue
		}

		if err, ok := errors.Fields[field.Name]; ok {
			data["Err_"+field.Name] = true
			switch err {
			case binding.RequireError:
				data["ErrorMsg"] = field.Name + " cannot be empty"
			case binding.AlphaDashError:
				data["ErrorMsg"] = field.Name + " must be valid alpha or numeric or dash(-_) characters"
			case binding.MinSizeError:
				data["ErrorMsg"] = field.Name + " must contain at least has " + getMinMaxSize(field) + " characters"
			case binding.MaxSizeError:
				data["ErrorMsg"] = field.Name + " must contain at most has " + getMinMaxSize(field) + " characters"
			case binding.EmailError:
				data["ErrorMsg"] = field.Name + " is not valid"
			default:
				data["ErrorMsg"] = "Unknown error: " + err
			}
			return
		}
	}
}

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.
func AssignForm(form interface{}, data base.TmplData) {
	typ := reflect.TypeOf(form)
	val := reflect.ValueOf(form)

	if typ.Kind() == reflect.Ptr {
		typ = typ.Elem()
		val = val.Elem()
	}

	for i := 0; i < typ.NumField(); i++ {
		field := typ.Field(i)

		fieldName := field.Tag.Get("form")
		// Allow ignored fields in the struct
		if fieldName == "-" {
			continue
		}

		data[fieldName] = val.Field(i).Interface()
	}
}