feat: add simple post validation

This commit is contained in:
Yanislav Igonin 2021-09-11 16:56:22 +03:00
parent e5bbc585e3
commit f24c97f5e0
3 changed files with 42 additions and 2 deletions

View File

@ -86,9 +86,14 @@ func CreateThread(c *gin.Context) {
} }
// TODO: dat shit crashes if no fields in request // TODO: dat shit crashes if no fields in request
// TODO: add validation (title or text)
text := form.Value["text"][0] text := form.Value["text"][0]
title := form.Value["title"][0] title := form.Value["title"][0]
isPostValid := Utils.ValidatePost(title, text)
if !isPostValid {
c.HTML(http.StatusBadRequest, "400.html", nil)
return
}
filesInRequest := form.File["files"] filesInRequest := form.File["files"]
conn, err := Db.Pool.Acquire(context.TODO()) conn, err := Db.Pool.Acquire(context.TODO())
@ -177,9 +182,14 @@ func UpdateThread(c *gin.Context) {
} }
// TODO: dat shit crashes if no fields in request // TODO: dat shit crashes if no fields in request
// TODO: add validation (title or text)
text := form.Value["text"][0] text := form.Value["text"][0]
title := form.Value["title"][0] title := form.Value["title"][0]
isPostValid := Utils.ValidatePost(title, text)
if !isPostValid {
c.HTML(http.StatusBadRequest, "400.html", nil)
return
}
filesInRequest := form.File["files"] filesInRequest := form.File["files"]
isSageField := form.Value["sage"] isSageField := form.Value["sage"]
var isSageString string var isSageString string

26
templates/400.html Normal file
View File

@ -0,0 +1,26 @@
<html lang="en">
<head>
{{ template "static" }}
<link
href="/static/error-image.css"
rel="stylesheet"
>
{{ template "static-meta" }}
</head>
<body>
<div class="h-100 d-flex justify-content-center align-items-center flex-column">
<h1>400</h1>
<h1>TITLE OR TEXT SHOULD NOT BE EMPTY</h1>
<a href="/" class="error-image-link">
<img
class="error-image"
src="/static/images/errors/404.png"
alt="Bad request"
>
</a>
</div>
</body>
</html>

View File

@ -46,3 +46,7 @@ func CreateThreadFolder(postID int) error {
return nil return nil
} }
func ValidatePost(title, text string) bool {
return (title == "" && text != "") || (title != "" && text == "")
}