ch8.5
This commit is contained in:
@@ -6,18 +6,17 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
|
"gitea.local.lab/Lbenedar/snippetbox/internal/models"
|
||||||
|
"gitea.local.lab/Lbenedar/snippetbox/internal/validator"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
type snippetCreateForm struct {
|
type snippetCreateForm struct {
|
||||||
Title string
|
Title string
|
||||||
Content string
|
Content string
|
||||||
Expires int
|
Expires int
|
||||||
FieldErrors map[string]string
|
validator.Validator
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
@@ -102,27 +101,17 @@ func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
form := snippetCreateForm{
|
form := snippetCreateForm{
|
||||||
Title: r.PostForm.Get("title"),
|
Title: r.PostForm.Get("title"),
|
||||||
Content: r.PostForm.Get("content"),
|
Content: r.PostForm.Get("content"),
|
||||||
Expires: expires,
|
Expires: expires,
|
||||||
FieldErrors: map[string]string{},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.TrimSpace(form.Title) == "" {
|
form.CheckField(validator.NotBlank(form.Title), "title", "This field cannot be blank")
|
||||||
form.FieldErrors["title"] = "This field cannot be blank"
|
form.CheckField(validator.MaxChars(form.Title, 100), "title", "This field cannot be more than 100 character long")
|
||||||
} else if utf8.RuneCountInString(form.Title) > 100 {
|
form.CheckField(validator.NotBlank(form.Content), "content", "This field cannot be blank")
|
||||||
form.FieldErrors["title"] = "This field cannot be more than 100 character long"
|
form.CheckField(validator.PermittedInt(form.Expires, 1, 7, 365), "expires", "This field must equal 1, 7 and 365")
|
||||||
}
|
|
||||||
|
|
||||||
if strings.TrimSpace(form.Content) == "" {
|
if !form.Valid() {
|
||||||
form.FieldErrors["content"] = "This field cannot be blank"
|
|
||||||
}
|
|
||||||
|
|
||||||
if form.Expires != 1 && form.Expires != 7 && form.Expires != 365 {
|
|
||||||
form.FieldErrors["expires"] = "This field must equal 1, 7 and 365"
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(form.FieldErrors) > 0 {
|
|
||||||
data := app.newTemplateData(r)
|
data := app.newTemplateData(r)
|
||||||
data.Form = form
|
data.Form = form
|
||||||
app.render(w, http.StatusUnprocessableEntity, "create.tmpl", data)
|
app.render(w, http.StatusUnprocessableEntity, "create.tmpl", data)
|
||||||
|
|||||||
46
internal/validator/validator.go
Normal file
46
internal/validator/validator.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package validator
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Validator struct {
|
||||||
|
FieldErrors map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Validator) Valid() bool {
|
||||||
|
return len(v.FieldErrors) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Validator) AddFieldError(key, message string) {
|
||||||
|
if v.FieldErrors == nil {
|
||||||
|
v.FieldErrors = make(map[string]string)
|
||||||
|
}
|
||||||
|
if _, exist := v.FieldErrors[key]; !exist {
|
||||||
|
v.FieldErrors[key] = message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *Validator) CheckField(ok bool, key, message string) {
|
||||||
|
if !ok {
|
||||||
|
v.AddFieldError(key, message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NotBlank(value string) bool {
|
||||||
|
return strings.TrimSpace(value) != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func MaxChars(value string, n int) bool {
|
||||||
|
return utf8.RuneCountInString(value) <= n
|
||||||
|
}
|
||||||
|
|
||||||
|
func PermittedInt(value int, permittedValues ...int) bool {
|
||||||
|
for i := range permittedValues {
|
||||||
|
if value == permittedValues[i] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user