|
|
|
@ -14,12 +14,17 @@
|
|
|
|
|
:options (spec/coll-of (spec/tuple string? string?))))
|
|
|
|
|
(spec/def ::widget
|
|
|
|
|
#(in? [:input :select :checkbox :textarea :mselect] %))
|
|
|
|
|
(spec/def ::from-req
|
|
|
|
|
(spec/and fn? #(= 1 (->> % meta :arglists (map count) first))))
|
|
|
|
|
(spec/def ::to-form ::from-req)
|
|
|
|
|
(spec/def ::field
|
|
|
|
|
(spec/keys :req-un [::label
|
|
|
|
|
::required
|
|
|
|
|
::spec]
|
|
|
|
|
:opt-un [::options
|
|
|
|
|
::widget]))
|
|
|
|
|
::widget
|
|
|
|
|
::from-req
|
|
|
|
|
::to-form]))
|
|
|
|
|
(spec/def ::fields
|
|
|
|
|
(spec/map-of keyword? ::field))
|
|
|
|
|
(spec/def ::form
|
|
|
|
@ -93,12 +98,13 @@
|
|
|
|
|
(and (some? value) (not= 0 value))
|
|
|
|
|
(if validate? req-value value))))
|
|
|
|
|
|
|
|
|
|
(defn render-textarea [id {:keys [label spec]} value req-value validate?]
|
|
|
|
|
(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 id
|
|
|
|
|
(hform/text-area {:required (when required "required")}
|
|
|
|
|
id
|
|
|
|
|
(if validate? req-value value))))
|
|
|
|
|
|
|
|
|
|
(defn render-select [id {:keys [label spec options]} value req-value validate?]
|
|
|
|
@ -144,7 +150,7 @@
|
|
|
|
|
validate? (= form-hash submitted-hash)]
|
|
|
|
|
(conj (map
|
|
|
|
|
(fn [[field-id field-def]]
|
|
|
|
|
(let [{:keys [options widget]} field-def
|
|
|
|
|
(let [{:keys [options widget to-form]} field-def
|
|
|
|
|
value (field-id values)
|
|
|
|
|
req-value (get-in req [:params field-id])
|
|
|
|
|
widget (cond (some? widget) widget
|
|
|
|
@ -153,9 +159,21 @@
|
|
|
|
|
widget-renderer (widget render-mapping)]
|
|
|
|
|
(widget-renderer field-id
|
|
|
|
|
field-def
|
|
|
|
|
value
|
|
|
|
|
(if to-form (to-form value) value)
|
|
|
|
|
req-value
|
|
|
|
|
validate?)))
|
|
|
|
|
(:fields form-def))
|
|
|
|
|
(hform/hidden-field "__form-hash" form-hash)
|
|
|
|
|
(anti-forgery-field))))
|
|
|
|
|
|
|
|
|
|
(defn form-data [form-def req]
|
|
|
|
|
{:pre [(spec/assert ::form form-def)]}
|
|
|
|
|
(reduce (fn [coll [id field]]
|
|
|
|
|
(let [value (get-in req [:params id])]
|
|
|
|
|
(assoc coll
|
|
|
|
|
id
|
|
|
|
|
(if-let [from-req (:from-req field)]
|
|
|
|
|
(from-req value)
|
|
|
|
|
value))))
|
|
|
|
|
{}
|
|
|
|
|
(:fields form-def)))
|
|
|
|
|