|
|
|
@ -82,71 +82,57 @@
|
|
|
|
|
true
|
|
|
|
|
(:params req)))
|
|
|
|
|
|
|
|
|
|
(defn render-input [id {:keys [label required spec]} value req-value validate?]
|
|
|
|
|
(list
|
|
|
|
|
(when (and validate? (not (spec/valid? spec req-value)))
|
|
|
|
|
(spec-to-errmsg label spec req-value))
|
|
|
|
|
(hform/label id label)
|
|
|
|
|
(hform/text-field {:required (or required false)}
|
|
|
|
|
id
|
|
|
|
|
(if validate? req-value value))))
|
|
|
|
|
|
|
|
|
|
(defn render-checkbox [id {:keys [label spec]} value req-value validate?]
|
|
|
|
|
(list
|
|
|
|
|
(when (and validate? (not (spec/valid? spec req-value)))
|
|
|
|
|
(spec-to-errmsg label spec value))
|
|
|
|
|
(hform/label id label)
|
|
|
|
|
(hform/check-box id
|
|
|
|
|
(and (some? value) (not= 0 value))
|
|
|
|
|
(if validate? req-value value))))
|
|
|
|
|
|
|
|
|
|
(defn render-textarea [id
|
|
|
|
|
{:keys [label required spec]}
|
|
|
|
|
value
|
|
|
|
|
req-value
|
|
|
|
|
validate?]
|
|
|
|
|
(list
|
|
|
|
|
(when (and validate? (not (spec/valid? spec req-value)))
|
|
|
|
|
(spec-to-errmsg label spec value))
|
|
|
|
|
(hform/label id label)
|
|
|
|
|
(hform/text-area {:required (or required false)}
|
|
|
|
|
(defn input-widget [id def value]
|
|
|
|
|
(hform/text-field {:required (or (:required def) false)}
|
|
|
|
|
id
|
|
|
|
|
(if validate? req-value value))))
|
|
|
|
|
|
|
|
|
|
(defn render-select [id {:keys [label spec options]} value req-value validate?]
|
|
|
|
|
(list
|
|
|
|
|
(when (and validate? (not (spec/valid? spec req-value)))
|
|
|
|
|
(spec-to-errmsg label spec value))
|
|
|
|
|
(hform/label id label)
|
|
|
|
|
(hform/drop-down id
|
|
|
|
|
options
|
|
|
|
|
(if validate? req-value value))))
|
|
|
|
|
|
|
|
|
|
(defn render-multiselect [id
|
|
|
|
|
{:keys [label spec options]}
|
|
|
|
|
value
|
|
|
|
|
req-value
|
|
|
|
|
validate?]
|
|
|
|
|
(list
|
|
|
|
|
(when (and validate? (not (spec/valid? spec req-value)))
|
|
|
|
|
(spec-to-errmsg label spec value))
|
|
|
|
|
(hform/label id label)
|
|
|
|
|
[:select {:multiple "multiple"
|
|
|
|
|
:size 5
|
|
|
|
|
:name (name id)
|
|
|
|
|
:id (name id)}
|
|
|
|
|
(for [option options]
|
|
|
|
|
[:option {:value (second option)
|
|
|
|
|
:selected (in? (if validate? req-value value)
|
|
|
|
|
(second option))}
|
|
|
|
|
(first option)])]))
|
|
|
|
|
|
|
|
|
|
(def render-mapping
|
|
|
|
|
{:input render-input
|
|
|
|
|
:checkbox render-checkbox
|
|
|
|
|
:textarea render-textarea
|
|
|
|
|
:select render-select
|
|
|
|
|
:mselect render-multiselect})
|
|
|
|
|
value))
|
|
|
|
|
|
|
|
|
|
(defn checkbox-widget [id def value]
|
|
|
|
|
(hform/check-box id
|
|
|
|
|
(and (some? value)
|
|
|
|
|
(not= 0 value))
|
|
|
|
|
value))
|
|
|
|
|
|
|
|
|
|
(defn textarea-widget [id def value]
|
|
|
|
|
(hform/text-area {:required (or (:required def) false)}
|
|
|
|
|
id
|
|
|
|
|
value))
|
|
|
|
|
|
|
|
|
|
(defn select-widget [id def value]
|
|
|
|
|
(hform/drop-down id
|
|
|
|
|
(:options def)
|
|
|
|
|
value))
|
|
|
|
|
|
|
|
|
|
(defn multiselect-widget [id def value]
|
|
|
|
|
(let [options (:options def)]
|
|
|
|
|
[:select {:multiple "multiple"
|
|
|
|
|
:size 5
|
|
|
|
|
:name (name id)
|
|
|
|
|
:id (name id)}
|
|
|
|
|
(for [option options]
|
|
|
|
|
[:option {:value (second option)
|
|
|
|
|
:selected (in? value (second option))}
|
|
|
|
|
(first option)])]))
|
|
|
|
|
|
|
|
|
|
(def widget-mapping
|
|
|
|
|
{:input input-widget
|
|
|
|
|
:checkbox checkbox-widget
|
|
|
|
|
:textarea textarea-widget
|
|
|
|
|
:select select-widget
|
|
|
|
|
:mselect multiselect-widget})
|
|
|
|
|
|
|
|
|
|
(defn widget
|
|
|
|
|
[id def value req-value validate?]
|
|
|
|
|
(let [{:keys [label spec widget options]} def
|
|
|
|
|
widget (cond (some? widget) widget
|
|
|
|
|
(some? options) :select
|
|
|
|
|
:else :input)
|
|
|
|
|
renderer (widget widget-mapping)]
|
|
|
|
|
(list
|
|
|
|
|
(when (and validate? (not (spec/valid? spec req-value)))
|
|
|
|
|
(spec-to-errmsg label spec req-value))
|
|
|
|
|
(hform/label id label)
|
|
|
|
|
(renderer id def (if validate? req-value value)))))
|
|
|
|
|
|
|
|
|
|
(defn form-hash [def values]
|
|
|
|
|
(str (hash [def values])))
|
|
|
|
@ -158,18 +144,14 @@
|
|
|
|
|
validate? (= form-hash submitted-hash)]
|
|
|
|
|
(conj (map
|
|
|
|
|
(fn [[field-id field-def]]
|
|
|
|
|
(let [{:keys [options widget to-form]} field-def
|
|
|
|
|
(let [{:keys [options to-form]} field-def
|
|
|
|
|
value (field-id values)
|
|
|
|
|
req-value (get-in req [:params field-id])
|
|
|
|
|
widget (cond (some? widget) widget
|
|
|
|
|
(some? options) :select
|
|
|
|
|
:else :input)
|
|
|
|
|
widget-renderer (widget render-mapping)]
|
|
|
|
|
(widget-renderer field-id
|
|
|
|
|
field-def
|
|
|
|
|
(if to-form (to-form value) value)
|
|
|
|
|
req-value
|
|
|
|
|
validate?)))
|
|
|
|
|
req-value (get-in req [:params field-id])]
|
|
|
|
|
(widget field-id
|
|
|
|
|
field-def
|
|
|
|
|
(if to-form (to-form value) value)
|
|
|
|
|
req-value
|
|
|
|
|
validate?)))
|
|
|
|
|
(:fields form-def))
|
|
|
|
|
(hform/hidden-field "__form-hash" form-hash)
|
|
|
|
|
(anti-forgery-field))))
|
|
|
|
|