Formative Demo

This is a demo of the Formative library. Submit the form to see how the data gets validated and processed.

Full demo source code

Section 1

Section 2

Please make note of this note.
(def demo-form
  {:fields [{:name :h1 :type :heading :text "Section 1"}
            {:name :full-name}
            {:name "user[email]" :type :email}
            {:name :spam :type :checkbox :label "Yes, please spam me."}
            {:name :password :type :password}
            {:name :password-confirm :type :password}
            {:name :h2 :type :heading :text "Section 2"}
            {:name :note :type :html
             :html [:div.alert.alert-info "Please make note of this note."]}
            {:name :date :type :date-select}
            {:name :time :type :time-select}
            {:name :flavors :type :checkboxes
             :options ["Chocolate" "Vanilla" "Strawberry" "Mint"]}
            {:name :location :type :compound
             :fields [{:name :city :placeholder "City" :class "input-medium"}
                      {:name :state :type :us-state :placeholder "Select a state"}]}
            {:name :picture :type :file :title "Choose a file"}]
   :validations [[:required [:full-name "user[email]" :password]]
                 [:min-length 4 :password]
                 [:equal [:password :password-confirm]]
                 [:min-length 2 :flavors "select two or more flavors"]
                 [:complete :location]
   :enctype "multipart/form-data"})

(defn show-demo-form [params & {:keys [problems]}]
  (let [now (java.util.Date.)
        defaults {:spam true
                  :date now
                  :time now}]
    (layout
      [:h1 "Formative Demo"]
      (f/render-form (assoc demo-form
                            :values (merge defaults params)
                            :problems problems)))))

(defn submit-demo-form [params]
  (fp/with-fallback #(show-demo-form params :problems %)
    (let [values (fp/parse-params demo-form params)]
      (layout
        [:h1 "Thank you!"]
        [:pre (prn-str values)]))))