|
|
@ -49,8 +49,8 @@
|
|
|
|
(:fields form-def)))
|
|
|
|
(:fields form-def)))
|
|
|
|
|
|
|
|
|
|
|
|
(spec/fdef form-data
|
|
|
|
(spec/fdef form-data
|
|
|
|
:args (spec/cat :form-def ::form :req map?)
|
|
|
|
:args (spec/cat :form-def ::form :req map?)
|
|
|
|
:ret map?)
|
|
|
|
:ret map?)
|
|
|
|
(spectest/instrument `form-data)
|
|
|
|
(spectest/instrument `form-data)
|
|
|
|
|
|
|
|
|
|
|
|
(defn form-specs-valid? [form-def req]
|
|
|
|
(defn form-specs-valid? [form-def req]
|
|
|
@ -63,14 +63,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
(defn field-specs-valid? [form-def req]
|
|
|
|
(defn field-specs-valid? [form-def req]
|
|
|
|
(reduce-kv
|
|
|
|
(reduce-kv
|
|
|
|
(fn [result field field-def]
|
|
|
|
(fn [result field field-def]
|
|
|
|
(if-let [field-spec (:spec field-def)]
|
|
|
|
(if-let [field-spec (:spec field-def)]
|
|
|
|
(if (spec/valid? field-spec (get-in req [:params field]))
|
|
|
|
(if (spec/valid? field-spec (get-in req [:params field]))
|
|
|
|
true
|
|
|
|
true
|
|
|
|
(reduced false))
|
|
|
|
(reduced false))
|
|
|
|
result))
|
|
|
|
result))
|
|
|
|
true
|
|
|
|
true
|
|
|
|
(:fields form-def)))
|
|
|
|
(:fields form-def)))
|
|
|
|
|
|
|
|
|
|
|
|
(defn valid? [form-def req]
|
|
|
|
(defn valid? [form-def req]
|
|
|
|
(let [data (form-data form-def req)]
|
|
|
|
(let [data (form-data form-def req)]
|
|
|
@ -78,8 +78,8 @@
|
|
|
|
(form-specs-valid? form-def req))))
|
|
|
|
(form-specs-valid? form-def req))))
|
|
|
|
|
|
|
|
|
|
|
|
(spec/fdef valid?
|
|
|
|
(spec/fdef valid?
|
|
|
|
:args (spec/cat :form-def ::form :req map?)
|
|
|
|
:args (spec/cat :form-def ::form :req map?)
|
|
|
|
:ret map?)
|
|
|
|
:ret map?)
|
|
|
|
(spectest/instrument `valid?)
|
|
|
|
(spectest/instrument `valid?)
|
|
|
|
|
|
|
|
|
|
|
|
(def ^:dynamic *row-theme* theme/row)
|
|
|
|
(def ^:dynamic *row-theme* theme/row)
|
|
|
@ -94,7 +94,7 @@
|
|
|
|
(def ^:dynamic *hidden-widget-theme* theme/hidden-widget)
|
|
|
|
(def ^:dynamic *hidden-widget-theme* theme/hidden-widget)
|
|
|
|
|
|
|
|
|
|
|
|
(defn widget-markup [values req validate? [id def]]
|
|
|
|
(defn widget-markup [values req validate? [id def]]
|
|
|
|
(let [{:keys [spec widget options to-form]} def
|
|
|
|
(let [{:keys [spec widget to-form]} def
|
|
|
|
value ((or to-form identity) (id values))
|
|
|
|
value ((or to-form identity) (id values))
|
|
|
|
req-value (get-in req [:params id])
|
|
|
|
req-value (get-in req [:params id])
|
|
|
|
renderer (case widget
|
|
|
|
renderer (case widget
|
|
|
@ -118,29 +118,49 @@
|
|
|
|
(= (form-hash form-def values)
|
|
|
|
(= (form-hash form-def values)
|
|
|
|
(get-in req [:params :__form-hash])))
|
|
|
|
(get-in req [:params :__form-hash])))
|
|
|
|
|
|
|
|
|
|
|
|
(defn render-widgets [form-def values req]
|
|
|
|
(defn render-widgets
|
|
|
|
(let [validate? (validate? form-def values req)
|
|
|
|
([form-def values req]
|
|
|
|
form-errors (when (and validate?
|
|
|
|
(render-widgets form-def values req
|
|
|
|
(not (form-specs-valid? form-def
|
|
|
|
{:render-anti-forgery-field? true}))
|
|
|
|
req)))
|
|
|
|
([form-def values req {raff? :render-anti-forgery-field?}]
|
|
|
|
(*form-error-theme* form-def req))
|
|
|
|
(let [validate? (validate? form-def values req)
|
|
|
|
widget-mapper (partial widget-markup values req validate?)
|
|
|
|
form-errors (when (and validate?
|
|
|
|
defined-widgets (map widget-mapper (:fields form-def))
|
|
|
|
(not (form-specs-valid? form-def
|
|
|
|
hash-field (hform/hidden-field "__form-hash"
|
|
|
|
req)))
|
|
|
|
(form-hash form-def values))
|
|
|
|
(*form-error-theme* form-def req))
|
|
|
|
all-widgets (conj defined-widgets
|
|
|
|
widget-mapper (partial widget-markup values req validate?)
|
|
|
|
hash-field
|
|
|
|
defined-widgets (map widget-mapper (:fields form-def))
|
|
|
|
(anti-forgery-field))]
|
|
|
|
hash-field (hform/hidden-field "__form-hash"
|
|
|
|
(if form-errors
|
|
|
|
(form-hash form-def values))
|
|
|
|
(concat (if (sequential? form-errors)
|
|
|
|
all-widgets (conj defined-widgets
|
|
|
|
form-errors
|
|
|
|
hash-field)
|
|
|
|
[form-errors])
|
|
|
|
all-widgets (if raff?
|
|
|
|
all-widgets)
|
|
|
|
(conj all-widgets (anti-forgery-field))
|
|
|
|
all-widgets)))
|
|
|
|
all-widgets)]
|
|
|
|
|
|
|
|
(if form-errors
|
|
|
|
|
|
|
|
(concat (if (sequential? form-errors)
|
|
|
|
|
|
|
|
form-errors
|
|
|
|
|
|
|
|
[form-errors])
|
|
|
|
|
|
|
|
all-widgets)
|
|
|
|
|
|
|
|
all-widgets))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(spec/def ::render-anti-forgery-field?
|
|
|
|
|
|
|
|
#(boolean? (boolean %)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(spec/def ::options-map
|
|
|
|
|
|
|
|
(spec/keys :req-un
|
|
|
|
|
|
|
|
[::render-anti-forgery-field?]))
|
|
|
|
|
|
|
|
|
|
|
|
(spec/fdef render-widgets
|
|
|
|
(spec/fdef render-widgets
|
|
|
|
:args (spec/cat :form-def ::form
|
|
|
|
:args (spec/or :three-params
|
|
|
|
:values (spec/or :no-values nil?
|
|
|
|
(spec/cat :form-def ::form
|
|
|
|
:values map?)
|
|
|
|
:values (spec/or :no-values nil?
|
|
|
|
:req map?))
|
|
|
|
:values map?)
|
|
|
|
|
|
|
|
:req map?)
|
|
|
|
|
|
|
|
:four-params
|
|
|
|
|
|
|
|
(spec/cat :form-def ::form
|
|
|
|
|
|
|
|
:values (spec/or :no-values nil?
|
|
|
|
|
|
|
|
:values map?)
|
|
|
|
|
|
|
|
:req map?
|
|
|
|
|
|
|
|
:options ::options-map)))
|
|
|
|
(spectest/instrument `render-widgets)
|
|
|
|
(spectest/instrument `render-widgets)
|
|
|
|