ch8.4
This commit is contained in:
@@ -13,6 +13,13 @@ import (
|
|||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type snippetCreateForm struct {
|
||||||
|
Title string
|
||||||
|
Content string
|
||||||
|
Expires int
|
||||||
|
FieldErrors map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
func (app *application) render(w http.ResponseWriter, status int, page string, data *templateData) {
|
func (app *application) render(w http.ResponseWriter, status int, page string, data *templateData) {
|
||||||
ts, ok := app.templateCache[page]
|
ts, ok := app.templateCache[page]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -74,6 +81,10 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
data := app.newTemplateData(r)
|
data := app.newTemplateData(r)
|
||||||
|
|
||||||
|
data.Form = snippetCreateForm{
|
||||||
|
Expires: 365,
|
||||||
|
}
|
||||||
|
|
||||||
app.render(w, http.StatusOK, "create.tmpl", data)
|
app.render(w, http.StatusOK, "create.tmpl", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,8 +94,6 @@ func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request
|
|||||||
app.clientError(w, http.StatusBadRequest)
|
app.clientError(w, http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
title := r.PostForm.Get("title")
|
|
||||||
content := r.PostForm.Get("content")
|
|
||||||
|
|
||||||
expires, err := strconv.Atoi(r.PostForm.Get("expires"))
|
expires, err := strconv.Atoi(r.PostForm.Get("expires"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -92,27 +101,35 @@ func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldErrors := make(map[string]string)
|
form := snippetCreateForm{
|
||||||
if strings.TrimSpace(title) == "" {
|
Title: r.PostForm.Get("title"),
|
||||||
fieldErrors["title"] = "This field cannot be blank"
|
Content: r.PostForm.Get("content"),
|
||||||
} else if utf8.RuneCountInString(title) > 100 {
|
Expires: expires,
|
||||||
fieldErrors["title"] = "This field cannot be more than 100 character long"
|
FieldErrors: map[string]string{},
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.TrimSpace(content) == "" {
|
if strings.TrimSpace(form.Title) == "" {
|
||||||
fieldErrors["content"] = "This field cannot be blank"
|
form.FieldErrors["title"] = "This field cannot be blank"
|
||||||
|
} else if utf8.RuneCountInString(form.Title) > 100 {
|
||||||
|
form.FieldErrors["title"] = "This field cannot be more than 100 character long"
|
||||||
}
|
}
|
||||||
|
|
||||||
if expires != 1 && expires != 7 && expires != 365 {
|
if strings.TrimSpace(form.Content) == "" {
|
||||||
fieldErrors["expires"] = "This field must equal 1, 7 and 365"
|
form.FieldErrors["content"] = "This field cannot be blank"
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(fieldErrors) > 0 {
|
if form.Expires != 1 && form.Expires != 7 && form.Expires != 365 {
|
||||||
fmt.Fprint(w, fieldErrors)
|
form.FieldErrors["expires"] = "This field must equal 1, 7 and 365"
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(form.FieldErrors) > 0 {
|
||||||
|
data := app.newTemplateData(r)
|
||||||
|
data.Form = form
|
||||||
|
app.render(w, http.StatusUnprocessableEntity, "create.tmpl", data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id, err := app.snippets.Insert(title, content, expires)
|
id, err := app.snippets.Insert(form.Title, form.Content, form.Expires)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.serverError(w, err)
|
app.serverError(w, err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ type templateData struct {
|
|||||||
CurrentYear int
|
CurrentYear int
|
||||||
Snippet *models.Snippet
|
Snippet *models.Snippet
|
||||||
Snippets []*models.Snippet
|
Snippets []*models.Snippet
|
||||||
|
Form any
|
||||||
}
|
}
|
||||||
|
|
||||||
func humanDate(t time.Time) string {
|
func humanDate(t time.Time) string {
|
||||||
|
|||||||
@@ -4,17 +4,26 @@
|
|||||||
<form action='/snippet/create' method='POST'>
|
<form action='/snippet/create' method='POST'>
|
||||||
<div>
|
<div>
|
||||||
<label>Title:</label>
|
<label>Title:</label>
|
||||||
<input type='text' name='title'>
|
{{with .Form.FieldErrors.title}}
|
||||||
|
<label class='error'>{{.}}</label>
|
||||||
|
{{end}}
|
||||||
|
<input type='text' name='title' value='{{.Form.Title}}'>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label>Content:</label>
|
<label>Content:</label>
|
||||||
<textarea name='content'></textarea>
|
{{with .Form.FieldErrors.content}}
|
||||||
|
<label class='error'>{{.}}</label>
|
||||||
|
{{end}}
|
||||||
|
<textarea name='content'>{{.Form.Content}}</textarea>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<label>Delete in:</label>
|
<label>Delete in:</label>
|
||||||
<input type='radio' name='expires' value='365' checked> One Year
|
{{with .Form.FieldErrors.expires}}
|
||||||
<input type='radio' name='expires' value='7'> One Week
|
<label class='error'>{{.}}</label>
|
||||||
<input type='radio' name='expires' value='1'> One Day
|
{{end}}
|
||||||
|
<input type='radio' name='expires' value='365' {{if (eq .Form.Expires 365)}}checked{{end}}> One Year
|
||||||
|
<input type='radio' name='expires' value='7' {{if (eq .Form.Expires 7)}}checked{{end}}> One Week
|
||||||
|
<input type='radio' name='expires' value='1' {{if (eq .Form.Expires 1)}}checked{{end}}> One Day
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<input type='submit' value='Publish snippet'>
|
<input type='submit' value='Publish snippet'>
|
||||||
|
|||||||
Reference in New Issue
Block a user