turn widget rendering upside down

integration-tests
Josha von Gizycki 6 years ago
parent 63bb45587c
commit 245342fa8f

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

Loading…
Cancel
Save