Guest User

Untitled

a guest
May 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. (defprotocol IField
  2. (set-value [this value])
  3.  
  4. (to-clojure [this])
  5.  
  6. (clean [this])
  7.  
  8.  
  9. )
  10.  
  11. (defrecord IntegerField [value value-clean errors]
  12. IField
  13.  
  14. (set-value [this value]
  15. (assoc this :value value))
  16.  
  17. (clean [this]
  18. (assoc this :value-clean (to-clojure this)))
  19.  
  20. (to-clojure [this]
  21.  
  22. (cond
  23.  
  24. (integer? value)
  25. value
  26.  
  27. (string? value)
  28. (-> value s/trim Integer/parseInt)
  29.  
  30. :else (throw (Exception. (format "Integer parse error: %s" value))))))
  31.  
  32. (defrecord BooleanField [value value-clean]
  33. IField
  34.  
  35. (set-value [this value]
  36. (assoc this :value value))
  37.  
  38. (to-clojure [this]
  39. (cond
  40.  
  41. (boolean? value)
  42. value
  43.  
  44. (contains? #{"true" "TRUE" "True" "yes" "YES" "Yes" "1"} value)
  45. true
  46.  
  47. (contains? #{"false" "FALSE" "False" "no" "NO" "No" "0"} value)
  48. false
  49.  
  50. :else (throw (Exception. (format "Boolean parse error: %s" value))))))
  51.  
  52.  
  53. (ns zenform.form
  54. (:require [zenform.field :as field]))
  55.  
  56. (defprotocol IForm
  57.  
  58. (set-value [this field value])
  59.  
  60. (set-values [this values])
  61.  
  62. (clean [this]))
  63.  
  64. (defrecord Form [fields]
  65.  
  66. IForm
  67.  
  68. (clean [this]
  69. (reduce
  70. (fn [this [field-id field-obj]]
  71. (update-in this [:fields field-id] field/clean))
  72. this fields))
  73.  
  74. (set-value [this field value]
  75. (update-in this [:fields field] field/set-value value))
  76.  
  77. (set-values [this values]
  78. (reduce
  79. (fn [this [field value]]
  80. (set-value this field value))
  81. this values)))
  82.  
  83.  
  84. (defprotocol IValidator
  85.  
  86.  
  87. (validate [this value]
  88.  
  89. ))
  90.  
  91. (defrecord RegexValidator [regex message]
  92. IValidator
  93.  
  94. (validate [this value]
  95. (re-matches regex value)))
  96.  
  97. (defrecord RangeValidator [val-min val-max message]
  98. IValidator
  99.  
  100. (validate [this value]
  101. (and (>= value val-min)
  102. (<= value val-max))))
  103.  
  104. (defprotocol IField
  105.  
  106. (on-change [this value])
  107.  
  108. (to-clojure [this])
  109.  
  110. (render [this])
  111.  
  112. (validate [this])
  113.  
  114.  
  115.  
  116. )
  117.  
  118. (defrecord IntegerField [id value value-clear name label validators errors]
  119. IField
  120.  
  121. (to-clojure [this]
  122. (Integer/parseInt (:value this)))
  123.  
  124. #_
  125. (on-change [this value]
  126. (set-value this value)
  127. (try
  128.  
  129.  
  130. )
  131.  
  132.  
  133. )
  134.  
  135. (render [this]
  136. [:div.foo.bar
  137. [:input {:id id
  138. :name name
  139. :on-blur (fn [e] (on-change this (-> e .-target .-value)))
  140. :value value}]])
  141.  
  142. )
  143.  
  144. #_
  145. (defrecored FormField [id form]
  146. IField
  147.  
  148. )
  149.  
  150. (defprotocol IWidget
  151. (render [this]))
  152.  
  153. (defrecord CalendarWidget [id name label inline?]
  154.  
  155. IWidget
  156.  
  157. (render [this]
  158. [:input {:on-blur (fn [e] [:zenform/on-change ])
  159. }
  160. "test"]
  161. )
  162.  
  163.  
  164. )
  165.  
  166. (defrecord CalendarField [id value name label validators errors widget]
  167.  
  168. IField
  169.  
  170.  
  171.  
  172.  
  173. )
  174.  
  175.  
  176. (defprotocol IForm
  177.  
  178. (set-values [this values])
  179.  
  180. (validate [this])
  181.  
  182. (ok? [this])
  183.  
  184. (get-errors [this])
  185.  
  186. (get-values [this])
  187.  
  188. (get-clean-values [this])
  189.  
  190. )
  191.  
  192. (defrecord Form [fields validators]
  193. IForm
  194.  
  195. (set-values [this values]
  196. (reduce
  197. (fn [result [id val]]
  198. )
  199.  
  200.  
  201. this values)
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212. )
  213.  
  214.  
  215.  
  216. )
Add Comment
Please, Sign In to add comment