Skip to content

Commit 1c8de2d

Browse files
committed
signup form handler
1 parent 8ddc0b4 commit 1c8de2d

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

cmd/web/handlers.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,30 @@ func DisableIndex(next http.Handler) http.Handler {
115115
}
116116

117117
func (app *App) SignupUser(w http.ResponseWriter, r *http.Request) {
118-
fmt.Fprint(w, "SignupUser")
118+
app.RenderHtml(w, r, "signup.page.html", &HtmlData{
119+
Form: &forms.SignupUser{},
120+
})
119121
}
120122

121123
func (app *App) CreateUser(w http.ResponseWriter, r *http.Request) {
122-
fmt.Fprint(w, "CreateUser")
124+
err := r.ParseForm()
125+
if err != nil {
126+
app.ClientError(w, http.StatusBadRequest)
127+
return
128+
}
129+
130+
form := &forms.SignupUser{
131+
Name: r.PostForm.Get("name"),
132+
Email: r.PostForm.Get("email"),
133+
Password: r.PostForm.Get("password"),
134+
}
135+
136+
if !form.Valid() {
137+
app.RenderHtml(w, r, "signup.page.html", &HtmlData{Form: form})
138+
return
139+
}
140+
141+
fmt.Fprintf(w, "CreateUser")
123142
}
124143

125144
func (app *App) LoginUser(w http.ResponseWriter, r *http.Request) {

pkg/forms/forms.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
package forms
22

33
import (
4+
"regexp"
45
"strings"
6+
"unicode/utf8"
57
)
68

9+
var rxEmail = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
10+
711
type NewSnippet struct {
812
Title string
913
Content string
1014
Expires string
1115
Failures map[string]string
1216
}
1317

18+
type SignupUser struct {
19+
Name string
20+
Email string
21+
Password string
22+
Failures map[string]string
23+
}
24+
1425
func (ns *NewSnippet) Valid() bool {
1526
ns.Failures = make(map[string]string)
1627

@@ -33,3 +44,27 @@ func (ns *NewSnippet) Valid() bool {
3344

3445
return len(ns.Failures) == 0
3546
}
47+
48+
func (su *SignupUser) Valid() bool {
49+
su.Failures = make(map[string]string)
50+
51+
if len(strings.TrimSpace(su.Name)) == 0 {
52+
su.Failures["Name"] = "Name is required"
53+
} else if len(strings.TrimSpace(su.Name)) > 50 || len(strings.TrimSpace(su.Name)) < 3 {
54+
su.Failures["Name"] = "Name cannot be longer than 50 and shorter than 3 characters"
55+
}
56+
57+
if len(strings.TrimSpace(su.Email)) == 0 {
58+
su.Failures["Email"] = "Email is required"
59+
} else if len(strings.TrimSpace(su.Email)) > 254 || !rxEmail.MatchString(su.Email) {
60+
su.Failures["Email"] = "Email is not valid"
61+
}
62+
63+
if len(strings.TrimSpace(su.Password)) == 0 {
64+
su.Failures["Password"] = "Password is required"
65+
} else if utf8.RuneCountInString(su.Password) < 8 {
66+
su.Failures["Password"] = "Password is too short"
67+
}
68+
69+
return len(su.Failures) == 0
70+
}

ui/html/signup.page.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{{define "page-title"}}Signup{{end}}
2+
3+
{{define "page-body"}}
4+
<form action="/user/signup" method="POST" novalidate>
5+
{{with .Form}}
6+
<div>
7+
<label>Name:</label>
8+
{{with .Failures.Name}}
9+
<label class="error">{{.}}</label>
10+
{{end}}
11+
<input type="text" name="name" value="{{.Name}}">
12+
</div>
13+
<div>
14+
<label>Email:</label>
15+
{{with .Failures.Email}}
16+
<label class="error">{{.}}</label>
17+
{{end}}
18+
<input type="email" name="email" value="{{.Email}}">
19+
</div>
20+
<div>
21+
<label>Password:</label>
22+
{{with .Failures.Password}}
23+
<label class="error">{{.}}</label>
24+
{{end}}
25+
<input type="password" name="password">
26+
</div>
27+
<div>
28+
<input type="submit" value="Signup">
29+
</div>
30+
{{end}}
31+
</form>
32+
{{- end}}
33+
34+
{{define "page-footer"}}
35+
Be prepared for initiation!
36+
{{- end}}

0 commit comments

Comments
 (0)