Add a validation for the create form.

This commit is contained in:
2024-04-19 09:20:57 +03:00
parent 634f7e41e2
commit f493b47320
5 changed files with 86 additions and 4 deletions
+48
View File
@@ -312,6 +312,54 @@
(res/status 500)
(res/send (ex-message err))))))))
(defn htmx-create-title-validation-handler-factory
[client]
(fn [req res]
(let [channel (chan)
title (.-title (.-body req))
content (.-content (.-body req))]
(go
(try
(let [error (cond
(= (count title) 0)
"Tittle must be nonempty!!!"
:else
nil)]
(res/status 200)
(.render res "create_title_validation"
(-> {:error error
:title title
:layout false}
(clj->js))))
(catch js/Error err
(res/status 500)
(res/send (ex-message err))))))))
(defn htmx-create-content-validation-handler-factory
[]
(fn [req res]
(let [channel (chan)
title (.-title (.-body req))
content (.-content (.-body req))]
(go
(try
(let [error (cond
(= (count content) 0)
"Content must be nonempty!!!"
:else
nil)]
(res/status 200)
(.render res "create_content_validation"
(-> {:error error
:content content
:layout false}
(clj->js))))
(catch js/Error err
(res/status 500)
(res/send (ex-message err))))))))
(defn htmx-create-article-handler-factory
[client]
(fn [req res]
+14
View File
@@ -111,6 +111,20 @@
(. app post "/htmx/search/articles/"
(handlers/htmx-search-articles-handler-factory client))
(. app post "/htmx/validation/create-title/"
handlers/is-user-authorized
(handlers/htmx-create-title-validation-handler-factory client))
(. app use "/htmx/validation/create-title/"
(handlers/user-not-authorized-factory))
(. app post "/htmx/validation/create-content/"
handlers/is-user-authorized
(handlers/htmx-create-content-validation-handler-factory))
(. app use "/htmx/validation/create-content/"
(handlers/user-not-authorized-factory))
(. app post "/htmx/articles/"
handlers/is-user-authorized
(handlers/htmx-create-article-handler-factory client))