-
Notifications
You must be signed in to change notification settings - Fork 14
How to create a form
Added for version: 0.4
Updated for version: 0.7
To create a form use the form-for function and pass the contents of the form as the body to the form-for function.
To create a form which points right back to the same action use:
(form-for "")
The body in the above form-for is an empty string. To add a button, use form-button and pass a value:
(form-for (form-button "Submit"))
You can point form-for to another action, say “create”, by passing an option map:
(form-for { :action "create" } (form-button "Submit"))
The option map takes the same options as url-for, but also includes :name and :html-options.
Name is used as the key for the params map passed to the target url. If name is not given, then the value of :controller is used as the name. If :controller is not set, then “record” is used as the name.
To set the name to “message” use:
(form-for { :name "message" } (form-button "Submit"))
The :html-options key allows you to set other form tag attributes.
For example, if you want to set the encoding type to multipart/form-data use:
(form-for { :html-options { :enctype "multipart/form-data" } } (form-button "Submit"))
The body of form-for must be a single form or a function.
The following does not work:
(form-for { :name "message" } [:p "I'm in a form!"] (form-button "Submit"))
If you attempt the form-for call above, you’ll get a wrong number of arguments error.
To add more than one form, you’ll need to wrap them in another form:
(form-for { :name "message" } [:div [:p "I'm in a form!"] (form-button "Submit"))]
Or, if you don’t want to add a div, you could use htmli to convert the body to a string:
(form-for { :name "message" } (htmli [:p "I'm in a form!"] (form-button "Submit")))
If you pass a function as the body, the body will be generated from the return value of the function. The function will be evaluated with a request-map which is the combination of the request-map and options passed to form-for.
A valid form-for body function is something like:
(defn body [] [:p "I'm a form for action: " (conjure.core.server.request/action)])
The following call to form-for will show what ever is in request-map as the action:
(form-for { :name "message" } body)
The following call to form-for will show “add” as the action:
(form-for { :action "add", :name "message" } body)