form definitions

integration-tests
Josha von Gizycki 7 years ago
parent d67290b4af
commit 2fdce3b2e7

@ -21,6 +21,14 @@ a:active {
text-decoration: underline;
}
h1 {
font-size: 1.5rem;
}
h2 {
font-size: 1.3rem;
}
.thin-border {
border: @border-stack;
}

@ -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])))))

@ -5,7 +5,16 @@
[hiccup.form :as hform]
[clojure.spec.alpha :as spec]
[wanijo.domain.schema :as domain-schemas]
[wanijo.view :as view]))
[wanijo.view :as view]
[wanijo.forms :as forms]))
(spec/def ::schema-name
(spec/and some? string? not-empty))
(def new-schema-form
{:fields {:schema-name {:label "Nimi"
:required true
:spec ::schema-name}}})
(defn- overview! [req]
(let [session (:session req)
@ -28,20 +37,10 @@
[:h1 "Pali sin e jaki ijo"]
(hform/form-to
[:post "/schema/new"]
(view/spec-to-errmsg ::new-schema req)
(hform/label "name" "Nimi")
(hform/text-field {:required "required"}
"schema-name"
(get-in req [:params :schema-name]))
(hform/submit-button "Kama")
(forms/field req new-schema-form :schema-name)
(hform/submit-button "Pali")
(anti-forgery-field))])))
(spec/def ::schema-name
(spec/and some? string? not-empty))
(spec/def ::new-schema
(spec/keys :req-un [::schema-name]))
(defn- new! [req]
(if (spec/valid? ::new-schema (:params req))
(resp/redirect "/schema")

@ -1,7 +1,6 @@
(ns wanijo.view
(:require [hiccup.page :refer [html5
include-css]]
[clojure.spec.alpha :as spec]))
include-css]]))
(defn btnlink
([target caption]
@ -47,16 +46,3 @@
[:section.flash--error
[:h2.flash__heading--error "Pakala"]
content])
(defn spec-to-errmsg [spec-key req]
(when-let [spec-data (and (not-empty (:params req))
(spec/explain-data spec-key (:params req)))]
(flash-error
(->> (:clojure.spec.alpha/problems spec-data)
(map
(fn [prob]
[:p
"Field \""
[:span.flash__field (first (:path prob))]
"\" must comply to "
[:span.flash__pred (:pred prob)]]))))))

Loading…
Cancel
Save