add required to render-textarea, prepare conversion functions in forms

integration-tests
Josha von Gizycki 6 years ago
parent 997004c882
commit d9f2cd0c47

@ -14,12 +14,17 @@
:options (spec/coll-of (spec/tuple string? string?)))) :options (spec/coll-of (spec/tuple string? string?))))
(spec/def ::widget (spec/def ::widget
#(in? [:input :select :checkbox :textarea :mselect] %)) #(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/def ::field
(spec/keys :req-un [::label (spec/keys :req-un [::label
::required ::required
::spec] ::spec]
:opt-un [::options :opt-un [::options
::widget])) ::widget
::from-req
::to-form]))
(spec/def ::fields (spec/def ::fields
(spec/map-of keyword? ::field)) (spec/map-of keyword? ::field))
(spec/def ::form (spec/def ::form
@ -93,12 +98,13 @@
(and (some? value) (not= 0 value)) (and (some? value) (not= 0 value))
(if validate? req-value 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 (list
(when (and validate? (not (spec/valid? spec req-value))) (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 (hform/text-area {:required (when required "required")}
id
(if validate? req-value value)))) (if validate? req-value value))))
(defn render-select [id {:keys [label spec options]} value req-value validate?] (defn render-select [id {:keys [label spec options]} value req-value validate?]
@ -144,7 +150,7 @@
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]} field-def (let [{:keys [options widget 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 (cond (some? widget) widget
@ -153,9 +159,21 @@
widget-renderer (widget render-mapping)] widget-renderer (widget render-mapping)]
(widget-renderer field-id (widget-renderer field-id
field-def field-def
value (if to-form (to-form value) value)
req-value req-value
validate?))) 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))))
(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)))

Loading…
Cancel
Save