parent
d67290b4af
commit
2fdce3b2e7
@ -0,0 +1,30 @@
|
||||
(ns wanijo.forms
|
||||
(:require [hiccup.form :as hform]
|
||||
[clojure.spec.alpha :as spec]
|
||||
[wanijo.view :as view]))
|
||||
|
||||
(defn spec-to-errmsg [label spec-data]
|
||||
(view/flash-error
|
||||
(map
|
||||
(fn [prob]
|
||||
[:p
|
||||
"Field "
|
||||
[:span.flash__field label]
|
||||
" must comply to "
|
||||
[:span.flash__pred (:pred prob)]])
|
||||
(:clojure.spec.alpha/problems spec-data))))
|
||||
|
||||
(defn field [req form-def field]
|
||||
(let [field-value (get-in req [:params field])
|
||||
field-def (get-in form-def [:fields field])
|
||||
{:keys [label required] spec-key :spec} field-def
|
||||
spec-valid? (or (empty? (:params req))
|
||||
(spec/valid? spec-key field-value))]
|
||||
(list
|
||||
(when-not spec-valid?
|
||||
(spec-to-errmsg label
|
||||
(spec/explain-data spec-key field-value)))
|
||||
(hform/label field label)
|
||||
(hform/text-field {:required (when required "required")}
|
||||
field
|
||||
(get-in req [:params field])))))
|
Loading…
Reference in new issue