codedreams / formula

Formula is a clojure library for html forms
3 stars 1 forks source link

Pre-filling form elements with data #2

Open piranha opened 11 years ago

piranha commented 11 years ago

It should be possible to fill form with data to handle case when you've checked incoming data, it contains errors and you need to display form to user for corrections - right now it'll be empty of data.

Maybe I just missed how do you pre-fill form with data from submission?

piranha commented 11 years ago

I worked around this situation with such code:

(defn data-applier
  [[_ name attrs :as field] data]
  (update-in field [2 :value]
             #(if (contains? data name) (data name) %)))

(def register-form-fields [...])

(defn register-form
  [data]
  (let [fields (map #(data-applier % data) register-form-fields)
        errors (when data (validate register-form-validators data))]
     (fform [:post "."] fields errors)))

I think it could be worth including such pattern either in library or in documentation.

Additionally, maybe you have some ideas on how do I group all those things (fields, validators, and actual form generation) together?

codedreams commented 11 years ago

The initial plan of Formula was to create an easier way to do forms, as well as create "formulas" that users could just copy and past into projects in the documentation. The formulas, if nothing else will show users how Formula can be used. I will update the documentation drastically within the next few days.