Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defprotocol IField
- (set-value [this value])
- (to-clojure [this])
- (clean [this])
- )
- (defrecord IntegerField [value value-clean errors]
- IField
- (set-value [this value]
- (assoc this :value value))
- (clean [this]
- (assoc this :value-clean (to-clojure this)))
- (to-clojure [this]
- (cond
- (integer? value)
- value
- (string? value)
- (-> value s/trim Integer/parseInt)
- :else (throw (Exception. (format "Integer parse error: %s" value))))))
- (defrecord BooleanField [value value-clean]
- IField
- (set-value [this value]
- (assoc this :value value))
- (to-clojure [this]
- (cond
- (boolean? value)
- value
- (contains? #{"true" "TRUE" "True" "yes" "YES" "Yes" "1"} value)
- true
- (contains? #{"false" "FALSE" "False" "no" "NO" "No" "0"} value)
- false
- :else (throw (Exception. (format "Boolean parse error: %s" value))))))
- (ns zenform.form
- (:require [zenform.field :as field]))
- (defprotocol IForm
- (set-value [this field value])
- (set-values [this values])
- (clean [this]))
- (defrecord Form [fields]
- IForm
- (clean [this]
- (reduce
- (fn [this [field-id field-obj]]
- (update-in this [:fields field-id] field/clean))
- this fields))
- (set-value [this field value]
- (update-in this [:fields field] field/set-value value))
- (set-values [this values]
- (reduce
- (fn [this [field value]]
- (set-value this field value))
- this values)))
- (defprotocol IValidator
- (validate [this value]
- ))
- (defrecord RegexValidator [regex message]
- IValidator
- (validate [this value]
- (re-matches regex value)))
- (defrecord RangeValidator [val-min val-max message]
- IValidator
- (validate [this value]
- (and (>= value val-min)
- (<= value val-max))))
- (defprotocol IField
- (on-change [this value])
- (to-clojure [this])
- (render [this])
- (validate [this])
- )
- (defrecord IntegerField [id value value-clear name label validators errors]
- IField
- (to-clojure [this]
- (Integer/parseInt (:value this)))
- #_
- (on-change [this value]
- (set-value this value)
- (try
- )
- )
- (render [this]
- [:div.foo.bar
- [:input {:id id
- :name name
- :on-blur (fn [e] (on-change this (-> e .-target .-value)))
- :value value}]])
- )
- #_
- (defrecored FormField [id form]
- IField
- )
- (defprotocol IWidget
- (render [this]))
- (defrecord CalendarWidget [id name label inline?]
- IWidget
- (render [this]
- [:input {:on-blur (fn [e] [:zenform/on-change ])
- }
- "test"]
- )
- )
- (defrecord CalendarField [id value name label validators errors widget]
- IField
- )
- (defprotocol IForm
- (set-values [this values])
- (validate [this])
- (ok? [this])
- (get-errors [this])
- (get-values [this])
- (get-clean-values [this])
- )
- (defrecord Form [fields validators]
- IForm
- (set-values [this values]
- (reduce
- (fn [result [id val]]
- )
- this values)
- )
- )
Add Comment
Please, Sign In to add comment