|
|
@ -39,9 +39,7 @@
|
|
|
|
(spec/explain-data spec-key field-value)))))
|
|
|
|
(spec/explain-data spec-key field-value)))))
|
|
|
|
|
|
|
|
|
|
|
|
(defn field-valid?[value spec-key req]
|
|
|
|
(defn field-valid?[value spec-key req]
|
|
|
|
(or (or (and (contains? req :form-params)
|
|
|
|
(or (empty? (:form-params req))
|
|
|
|
(empty? (:form-params req)))
|
|
|
|
|
|
|
|
(empty? req))
|
|
|
|
|
|
|
|
(spec/valid? spec-key value)))
|
|
|
|
(spec/valid? spec-key value)))
|
|
|
|
|
|
|
|
|
|
|
|
(defn field
|
|
|
|
(defn field
|
|
|
@ -109,38 +107,40 @@
|
|
|
|
true
|
|
|
|
true
|
|
|
|
(:params req)))
|
|
|
|
(:params req)))
|
|
|
|
|
|
|
|
|
|
|
|
(defn render-input [id {:keys [label required spec]} values]
|
|
|
|
(defn render-input [id {:keys [label required spec]} value req-value validate?]
|
|
|
|
(let [value (id values)]
|
|
|
|
(list
|
|
|
|
(list (when-not (field-valid? value spec values)
|
|
|
|
(when (and validate? (not (spec/valid? spec req-value)))
|
|
|
|
(spec-to-errmsg label spec value))
|
|
|
|
(spec-to-errmsg label spec req-value))
|
|
|
|
(hform/label id label)
|
|
|
|
(hform/label id label)
|
|
|
|
(hform/text-field {:required (when required "required")}
|
|
|
|
(hform/text-field {:required (when required "required")}
|
|
|
|
id
|
|
|
|
id
|
|
|
|
value))))
|
|
|
|
(if validate? req-value value))))
|
|
|
|
|
|
|
|
|
|
|
|
(defn render-checkbox [id {:keys [label spec]} values]
|
|
|
|
(defn render-checkbox [id {:keys [label spec]} value req-value validate?]
|
|
|
|
(let [value (id values)]
|
|
|
|
|
|
|
|
(list
|
|
|
|
(list
|
|
|
|
(when-not (field-valid? value spec values)
|
|
|
|
(when (and validate? (not (spec/valid? spec req-value)))
|
|
|
|
(spec-to-errmsg label spec value))
|
|
|
|
(spec-to-errmsg label spec value))
|
|
|
|
(hform/label id label)
|
|
|
|
(hform/label id label)
|
|
|
|
(hform/check-box id (some? value) value))))
|
|
|
|
(hform/check-box id
|
|
|
|
|
|
|
|
(and (some? value) (not= 0 value))
|
|
|
|
|
|
|
|
(if validate? req-value value))))
|
|
|
|
|
|
|
|
|
|
|
|
(defn render-textarea [id {:keys [label spec]} values]
|
|
|
|
(defn render-textarea [id {:keys [label spec]} value req-value validate?]
|
|
|
|
(let [value (id values)]
|
|
|
|
|
|
|
|
(list
|
|
|
|
(list
|
|
|
|
(when-not (field-valid? value spec values)
|
|
|
|
(when (and validate? (not (spec/valid? spec req-value)))
|
|
|
|
(spec-to-errmsg label spec value))
|
|
|
|
(spec-to-errmsg label spec value))
|
|
|
|
(hform/label id label)
|
|
|
|
(hform/label id label)
|
|
|
|
(hform/text-area id value))))
|
|
|
|
(hform/text-area id
|
|
|
|
|
|
|
|
(if validate? req-value value))))
|
|
|
|
|
|
|
|
|
|
|
|
(defn render-select [id {:keys [label spec options]} values]
|
|
|
|
(defn render-select [id {:keys [label spec options]} value req-value validate?]
|
|
|
|
(let [value (id values)]
|
|
|
|
|
|
|
|
(list
|
|
|
|
(list
|
|
|
|
(when-not (field-valid? value spec values)
|
|
|
|
(when (and validate? (not (spec/valid? spec req-value)))
|
|
|
|
(spec-to-errmsg label spec value))
|
|
|
|
(spec-to-errmsg label spec value))
|
|
|
|
(hform/label id label)
|
|
|
|
(hform/label id label)
|
|
|
|
(hform/drop-down id options value))))
|
|
|
|
(hform/drop-down id
|
|
|
|
|
|
|
|
options
|
|
|
|
|
|
|
|
(if validate? req-value value))))
|
|
|
|
|
|
|
|
|
|
|
|
(def render-mapping
|
|
|
|
(def render-mapping
|
|
|
|
{:input render-input
|
|
|
|
{:input render-input
|
|
|
@ -148,29 +148,25 @@
|
|
|
|
:textarea render-textarea
|
|
|
|
:textarea render-textarea
|
|
|
|
:select render-select})
|
|
|
|
:select render-select})
|
|
|
|
|
|
|
|
|
|
|
|
(defn cleaned-values
|
|
|
|
(defn render-form [form-def values req]
|
|
|
|
"Removes keys from value map that are not configured in the form"
|
|
|
|
|
|
|
|
[form-def values]
|
|
|
|
|
|
|
|
(reduce-kv
|
|
|
|
|
|
|
|
(fn [agg value-key value]
|
|
|
|
|
|
|
|
(if (contains? (:fields form-def) value-key)
|
|
|
|
|
|
|
|
(assoc agg value-key value)
|
|
|
|
|
|
|
|
agg))
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
values))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defn render-form [form-def values]
|
|
|
|
|
|
|
|
{:pre [(spec/valid? ::form form-def)]}
|
|
|
|
{:pre [(spec/valid? ::form form-def)]}
|
|
|
|
(let [cleaned-values (cleaned-values form-def values)]
|
|
|
|
(let [form-hash (str (hash [form-def values]))
|
|
|
|
|
|
|
|
submitted-hash (get-in req [:params :__form-hash])
|
|
|
|
|
|
|
|
validate? (= form-hash submitted-hash)]
|
|
|
|
(conj (map
|
|
|
|
(conj (map
|
|
|
|
(fn [[field-id field-def]]
|
|
|
|
(fn [[field-id field-def]]
|
|
|
|
(let [{:keys [options widget]} field-def
|
|
|
|
(let [{:keys [options widget]} field-def
|
|
|
|
|
|
|
|
value (field-id values)
|
|
|
|
|
|
|
|
req-value (get-in req [:params field-id])
|
|
|
|
widget (cond (some? widget) widget
|
|
|
|
widget (cond (some? widget) widget
|
|
|
|
(some? options) :select
|
|
|
|
(some? options) :select
|
|
|
|
:else :input)
|
|
|
|
:else :input)
|
|
|
|
widget-renderer (widget render-mapping)]
|
|
|
|
widget-renderer (widget render-mapping)]
|
|
|
|
(widget-renderer field-id
|
|
|
|
(widget-renderer field-id
|
|
|
|
field-def
|
|
|
|
field-def
|
|
|
|
cleaned-values)))
|
|
|
|
value
|
|
|
|
|
|
|
|
req-value
|
|
|
|
|
|
|
|
validate?)))
|
|
|
|
(:fields form-def))
|
|
|
|
(:fields form-def))
|
|
|
|
|
|
|
|
(hform/hidden-field "__form-hash" form-hash)
|
|
|
|
(anti-forgery-field))))
|
|
|
|
(anti-forgery-field))))
|
|
|
|