lets-go:8.4 form repopulation
This commit is contained in:
parent
5316e01cce
commit
fd514a4580
@ -56,9 +56,20 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
||||
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
||||
data := app.newTemplateData(r)
|
||||
|
||||
data.Form = snippetCreateForm{
|
||||
Expires: 365,
|
||||
}
|
||||
|
||||
app.render(w, r, http.StatusOK, "create.tmpl", data)
|
||||
}
|
||||
|
||||
type snippetCreateForm struct {
|
||||
Title string
|
||||
Content string
|
||||
Expires int
|
||||
FieldErrors map[string]string
|
||||
}
|
||||
|
||||
// snippetCreatePost ...
|
||||
func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
@ -67,37 +78,41 @@ func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
|
||||
title := r.PostForm.Get("title")
|
||||
content := r.PostForm.Get("content")
|
||||
|
||||
expires, err := strconv.Atoi(r.PostForm.Get("expires"))
|
||||
if err != nil {
|
||||
app.clientError(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
if strings.TrimSpace(title) == "" {
|
||||
fieldErrors["title"] = "This field cannot be blank"
|
||||
} else if utf8.RuneCountInString(title) > 100 {
|
||||
fieldErrors["title"] = "This field cannot contain more than 100 characters"
|
||||
form := snippetCreateForm{
|
||||
Title: r.PostForm.Get("title"),
|
||||
Content: r.PostForm.Get("content"),
|
||||
Expires: expires,
|
||||
FieldErrors: map[string]string{},
|
||||
}
|
||||
|
||||
if strings.TrimSpace(content) == "" {
|
||||
fieldErrors["content"] = "This field cannot be blank"
|
||||
if strings.TrimSpace(form.Title) == "" {
|
||||
form.FieldErrors["title"] = "This field cannot be blank"
|
||||
} else if utf8.RuneCountInString(form.Title) > 100 {
|
||||
form.FieldErrors["title"] = "This field cannot contain more than 100 characters"
|
||||
}
|
||||
|
||||
if expires != 1 && expires != 7 && expires != 365 {
|
||||
fieldErrors["expires"] = "This field must equal 1, 7 or 365"
|
||||
if strings.TrimSpace(form.Content) == "" {
|
||||
form.FieldErrors["content"] = "This field cannot be blank"
|
||||
}
|
||||
|
||||
if len(fieldErrors) > 0 {
|
||||
fmt.Fprint(w, fieldErrors)
|
||||
if form.Expires != 1 && form.Expires != 7 && form.Expires != 365 {
|
||||
form.FieldErrors["expires"] = "This field must equal 1, 7 or 365"
|
||||
}
|
||||
|
||||
if len(form.FieldErrors) > 0 {
|
||||
data := app.newTemplateData(r)
|
||||
data.Form = form
|
||||
app.render(w, r, http.StatusUnprocessableEntity, "create.tmpl", data)
|
||||
return
|
||||
}
|
||||
|
||||
id, err := app.snippets.Insert(title, content, expires)
|
||||
id, err := app.snippets.Insert(form.Title, form.Content, form.Expires)
|
||||
if err != nil {
|
||||
app.serverError(w, r, err)
|
||||
return
|
||||
|
@ -12,6 +12,7 @@ type templateData struct {
|
||||
CurrentYear int
|
||||
Snippet models.Snippet
|
||||
Snippets []models.Snippet
|
||||
Form any
|
||||
}
|
||||
|
||||
// humanDate ...
|
||||
|
@ -4,17 +4,26 @@
|
||||
<form action='/snippet/create' method='POST'>
|
||||
<div>
|
||||
<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>
|
||||
<label>Content:</label>
|
||||
{{with .Form.FieldErrors.content}}
|
||||
<label class='error'>{{.}}</label>
|
||||
{{end}}
|
||||
<textarea name='content'></textarea>
|
||||
</div>
|
||||
<div>
|
||||
<label>Delete in:</label>
|
||||
<input type='radio' name='expires' value='365' checked> One Year
|
||||
<input type='radio' name='expires' value='7'> One Week
|
||||
<input type='radio' name='expires' value='1'> One Day
|
||||
{{with .Form.FieldErrors.expires}}
|
||||
<label class='error'>{{.}}</label>
|
||||
{{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>
|
||||
<input type='submit' value='Publish Snippet'>
|
||||
|
Loading…
Reference in New Issue
Block a user