Skip to content

Instantly share code, notes, and snippets.

@wilkerlucio
Created May 25, 2014 20:43
Show Gist options
  • Select an option

  • Save wilkerlucio/dfc0553c44577b0c1d21 to your computer and use it in GitHub Desktop.

Select an option

Save wilkerlucio/dfc0553c44577b0c1d21 to your computer and use it in GitHub Desktop.
(page "index.html")
(defelem cell-input [{:keys [cell] :as attrs}]
(let [target-value #(do! (-> % .-currentTarget) :value)]
(input :type (:type attrs "text")
:value cell
:on-input #(reset! cell (target-value %)))))
(defn form-input
([& {:keys [validator] :or {validator (fn [_] true)}}]
(let [c (cell nil)]
[c (cell= {:dirty? (if (nil? c) false true)
:valid? (if (validator c) true false)})])))
(defelem bootstrap-input [{[c details] :field :as attrs}]
((div :class "form-group"
(if (:label attrs) (label (:label attrs)))
((cell-input (assoc attrs :cell c)) :do-class {:form-control true}))
:do-class (cell= {:has-error (and (:dirty? details) (not (:valid? details)))})))
(html
(head
(title "Hoplon • Validated Form")
(link :rel "stylesheet" :href "bootstrap.min.css")
(link :rel "stylesheet" :href "bootstrap-theme.min.css"))
(body
(div :class "container"
(form
(bootstrap-input :field (form-input :validator seq) :label "Name")
(bootstrap-input :field (form-input :validator seq) :label "Email" :type "email")
))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment