From fb9933097bd313ffd1b86b752a58ff0b9e398f2f Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Tue, 12 Apr 2022 11:39:02 +0300 Subject: [PATCH] add validation on post create --- controllers/threads_controller.go | 26 ++++++++++--- repositories/structs.go | 24 ++++++++---- static/styles/post-form.css | 6 ++- templates/components/post-form.html | 5 ++- utils/utils.go | 59 +++++++++++++++++++++++++---- 5 files changed, 98 insertions(+), 22 deletions(-) diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 8320e3b..bf22c76 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -244,12 +244,28 @@ func UpdateThread(c *fiber.Ctx) error { // TODO: dat shit crashes if no fields in request text := form.Value["text"][0] filesInRequest := form.File["files"] - validationErrorMessage := utils.ValidatePost("", text, filesInRequest) - if validationErrorMessage != "" { - errorHtmlData := repositories.BadRequestHtmlData{ - Message: validationErrorMessage, + + validationErrors := utils.ValidatePost2("", text, filesInRequest) + if validationErrors != nil { + thread, err := repositories.Posts.GetThreadByPostID(threadID) + if err != nil { + log.Println("error:", err) + return c.Status(fiber.StatusInternalServerError).Render("pages/500", nil) } - return c.Status(fiber.StatusBadRequest).Render("pages/400", errorHtmlData) + + firstPost := thread[0] + captchaID := form.Value["captchaId"][0] + htmlData := repositories.GetThreadHtmlData{ + Thread: thread, + FormData: repositories.HtmlFormData{ + FirstPostID: firstPost.ID, + CaptchaID: captchaID, + IsCaptchaActive: config.App.IsCaptchaActive, + Errors: *validationErrors, + }, + } + + return c.Render("pages/thread", htmlData) } if config.App.IsCaptchaActive { diff --git a/repositories/structs.go b/repositories/structs.go index 121f908..0dd975c 100644 --- a/repositories/structs.go +++ b/repositories/structs.go @@ -39,11 +39,19 @@ type File struct { // HTML Templates Structs // HTML Templates Structs +// post-form.html +type HtmlFormErrors struct { + Title string + Text string + Files string +} + // post-form.html type HtmlFormData struct { FirstPostID int CaptchaID string IsCaptchaActive bool + Errors HtmlFormErrors } // index.html @@ -70,11 +78,11 @@ type BadRequestHtmlData struct { Message string } -const InvalidCaptchaErrorMessage = "INVALID CAPTCHA" -const InvalidTextOrFilesErrorMessage = "TEXT OR FILES SHOULD NOT BE EMPTY" -const InvalidTitleLengthErrorMessage = "TITLE SHOULD NOT EXCEED 100 CHARS" -const InvalidTextLengthErrorMessage = "TEXT SHOULD NOT EXCEED 1000 CHARS" -const InvalidFilesLengthErrorMessage = "MAXIMUM 4 FILES CAN BE UPLOADED" -const InvalidFileSizeErrorMessage = "FILE SIZE EXCIDED (3MB PER FILE)" -const InvalidFileExtErrorMessage = "AVALIABLE FILE EXT: PNG, JPG" -const ThreadIsArchivedErrorMessage = "THREAD IS ARCHIVED" +const InvalidCaptchaErrorMessage = "Invalid captcha" +const InvalidTextOrFilesErrorMessage = "Text or files should not be empty" +const InvalidTitleLengthErrorMessage = "Title should not exceed 100 chars" +const InvalidTextLengthErrorMessage = "Text should not exceed 1000 chars" +const InvalidFilesLengthErrorMessage = "Maximum 4 files can be uploaded" +const InvalidFileSizeErrorMessage = "File size exceeded (3MB PER FILE)" +const InvalidFileExtErrorMessage = "Available file ext: PNG, JPG" +const ThreadIsArchivedErrorMessage = "Thread is archived" diff --git a/static/styles/post-form.css b/static/styles/post-form.css index 1222f54..782d80f 100644 --- a/static/styles/post-form.css +++ b/static/styles/post-form.css @@ -1,3 +1,7 @@ -#postForm > * { +.form-control { margin-bottom: 1rem; +} + +.form-error-label { + color: #f00; } \ No newline at end of file diff --git a/templates/components/post-form.html b/templates/components/post-form.html index 0bde441..78c190d 100644 --- a/templates/components/post-form.html +++ b/templates/components/post-form.html @@ -9,17 +9,20 @@ {{ end }} {{ if eq .FirstPostID 0 }} + {{ end }} + + {{ if .IsCaptchaActive }} {{ template "captcha" .CaptchaID }} {{ end }} -
+
{{ if ne .FirstPostID 0 }}
diff --git a/utils/utils.go b/utils/utils.go index e00a325..e4b16ce 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -6,7 +6,7 @@ import ( "image" "image/jpeg" "image/png" - Repositories "micrach/repositories" + "micrach/repositories" "mime/multipart" "os" "path/filepath" @@ -70,36 +70,81 @@ func CreateThreadFolder(postID int) error { return nil } +// TODO: Delete after func ValidatePost(title, text string, files []*multipart.FileHeader) string { if text == "" && len(files) == 0 { - return Repositories.InvalidTextOrFilesErrorMessage + return repositories.InvalidTextOrFilesErrorMessage } if len([]rune(title)) > 100 { - return Repositories.InvalidTitleLengthErrorMessage + return repositories.InvalidTitleLengthErrorMessage } if len([]rune(text)) > 1000 { - return Repositories.InvalidTextLengthErrorMessage + return repositories.InvalidTextLengthErrorMessage } if len(files) > 4 { - return Repositories.InvalidFilesLengthErrorMessage + return repositories.InvalidFilesLengthErrorMessage } isFilesExtsValid := CheckFilesExt(files) if !isFilesExtsValid { - return Repositories.InvalidFileExtErrorMessage + return repositories.InvalidFileExtErrorMessage } isFilesSizesNotToBig := CheckFilesSize(files) if !isFilesSizesNotToBig { - return Repositories.InvalidFileSizeErrorMessage + return repositories.InvalidFileSizeErrorMessage } return "" } +func ValidatePost2(title, text string, files []*multipart.FileHeader) *repositories.HtmlFormErrors { + validationError := new(repositories.HtmlFormErrors) + hasErrors := false + + if text == "" && len(files) == 0 { + validationError.Text = repositories.InvalidTextOrFilesErrorMessage + validationError.Files = repositories.InvalidTextOrFilesErrorMessage + hasErrors = true + } + + if len([]rune(title)) > 100 { + validationError.Title = repositories.InvalidTitleLengthErrorMessage + hasErrors = true + } + + if len([]rune(text)) > 1000 { + validationError.Text = repositories.InvalidTextLengthErrorMessage + hasErrors = true + } + + if len(files) > 4 { + validationError.Files = repositories.InvalidFilesLengthErrorMessage + hasErrors = true + } + + isFilesExtsValid := CheckFilesExt(files) + if !isFilesExtsValid { + validationError.Files = repositories.InvalidFileExtErrorMessage + hasErrors = true + } + + isFilesSizesNotToBig := CheckFilesSize(files) + if !isFilesSizesNotToBig { + validationError.Files = repositories.InvalidFileSizeErrorMessage + hasErrors = true + } + + if hasErrors { + return validationError + } + + return nil +} + func CheckFilesSize(files []*multipart.FileHeader) bool { for _, file := range files { if file.Size > int64(FILE_SIZE_IN_BYTES) {