From 6ea09ca7f4e6b06930f3e1b4d940b0db2d632868 Mon Sep 17 00:00:00 2001 From: Josha von Gizycki Date: Sun, 30 Sep 2018 22:53:07 +0200 Subject: [PATCH] add hidden widget --- src/formulare/core.clj | 6 +++- test/formulare/core_test.clj | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/formulare/core.clj b/src/formulare/core.clj index 51ef9ff..9f4dd93 100644 --- a/src/formulare/core.clj +++ b/src/formulare/core.clj @@ -94,12 +94,16 @@ :selected (in? value (second option))} (first option)])])) +(defn hidden-widget [id def value] + (hform/hidden-field id value)) + (def widget-mapping {:input input-widget :checkbox checkbox-widget :textarea textarea-widget :select select-widget - :mselect multiselect-widget}) + :mselect multiselect-widget + :hidden hidden-widget}) (defn widget-markup [id def value req-value validate?] diff --git a/test/formulare/core_test.clj b/test/formulare/core_test.clj index 8a41e88..64dbb15 100644 --- a/test/formulare/core_test.clj +++ b/test/formulare/core_test.clj @@ -186,6 +186,31 @@ (nthrest (render-widgets def {} {}) 2)))))) + (testing "hidden is rendered correctly" + (let [def {:fields {:foo-id {:label "foo-label" + :widget :hidden}}}] + (is (= (list nil + [:label {:for "foo-id"} "foo-label"] + [:input {:id "foo-id" + :type "hidden" + :name "foo-id" + :value nil}]) + (first + (nthrest + (render-widgets def {} {}) + 2))))) + (let [def {:fields {:foo-id {:label "foo-label" + :widget :hidden}}}] + (is (= (list nil + [:label {:for "foo-id"} "foo-label"] + [:input {:id "foo-id" + :type "hidden" + :name "foo-id" + :value "4711"}]) + (first + (nthrest + (render-widgets def {:foo-id "4711"} {}) + 2)))))) (testing "mselect is rendered correctly" (let [def {:fields {:foo-id {:label "foo-label" :widget :mselect @@ -204,3 +229,32 @@ (nthrest (render-widgets def {} {}) 2))))))) + +(deftest to-form-is-used + (testing "data passed to fo-form is taken from values" + (let [def {:fields {:foo {:label "" + :to-form (partial str "4711")}}}] + (is (= (list nil + [:label {:for "foo"} ""] + [:input {:id "foo" + :name "foo" + :value "4711abc" + :required false + :type "text"}]) + (first + (nthrest + (render-widgets def {:foo "abc"} {}) + 2))))) + (let [def {:fields {:foo {:label "" + :to-form (partial str "4711")}}}] + (is (= (list nil + [:label {:for "foo"} ""] + [:input {:id "foo" + :name "foo" + :value "4711abc" + :required false + :type "text"}]) + (first + (nthrest + (render-widgets def {:foo "abc"} {:params {:foo "def"}}) + 2)))))))