Skip to content

Commit

Permalink
Add form-with helper
Browse files Browse the repository at this point in the history
  • Loading branch information
swlkr committed Feb 17, 2020
1 parent 3492c2e commit 1d43a90
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/joy/form-helper.janet
@@ -1,5 +1,6 @@
(import ./router :as router)
(import ./middleware :as middleware)
(import ./helper :prefix "")


(defn- field [kind val key & attrs]
Expand Down Expand Up @@ -100,6 +101,48 @@
(hidden-field action :_method))]))


(defn form-with
[request &opt options & body]
`
Generates an html <form> element where the request is the request dictionary and options
are any form options.

Options can look like this:

{:route <a route keyword>
:route [:route {:id 1}] <- routes with args
:method "method"
:action "/"
:class ""
:enctype ""}

Examples:

(form-with request {:route :account/new :enctype "multipart/form-data"}
(label :name "name")
(file-field {} :name)
(submit "Upload file"))

(form-with request (merge (action-for :account/edit {:id 1}) {:enctype "multipart/form-data"})
(label :name "name")
(file-field {} :name)
(submit "Upload file"))`
(default options {})
(let [{:action action :route route} options
action (if (truthy? action)
{:action action}
(if (truthy? route)
(router/action-for ;(if (indexed? route) route [route]))
{:action ""}))
options (select-keys options [:class :enctype :method])]
[:form (merge options action)
body
(when (get request :csrf-token)
[:input {:type "hidden" :name "__csrf-token" :value (middleware/form-csrf-token request)}])
(when (truthy? (get action :_method))
(hidden-field action :_method))]))


(defn label
`Generates a <label> html element where html-for
is the for attribute value (as a keyword) and the
Expand Down

0 comments on commit 1d43a90

Please sign in to comment.