Skip to content

Instantly share code, notes, and snippets.

@ibdknox ibdknox/errors.clj
Created Jul 24, 2011

Embed
What would you like to do?
forms
(defpartial error-item [[first-error]]
[:p.error first-error])
(defpartial user-fields [{:keys [firstname lastname]}]
(vali/on-error :firstname error-item)
(label "firstname" "First name: ")
(text-field "firstname" firstname)
(vali/on-error :lastname error-item)
(label "lastname" "Last name: ")
(text-field "lastname" lastname))
(use 'noir.core 'hiccup.page-helpers 'hiccup.form-helpers)
(require '[noir.validation :as vali])
(require '[noir.response :as resp])
(defpartial layout [& content]
(html5
[:head
[:title "Forms"]]
[:body
content]))
(defpartial error-item [[first-error]]
[:p.error first-error])
(defpartial user-fields [{:keys [firstname lastname]}]
(vali/on-error :firstname error-item)
(label "firstname" "First name: ")
(text-field "firstname" firstname)
(vali/on-error :lastname error-item)
(label "lastname" "Last name: ")
(text-field "lastname" lastname))
(defn valid? [{:keys [firstname lastname]}]
(vali/rule (vali/min-length? firstname 5)
[:firstname "Your first name must have more than 5 letters."])
(vali/rule (vali/has-value? lastname)
[:lastname "You must have a last name"])
(not (vali/errors? :lastname :firstname)))
(defpage "/user/add" {:as user}
(common/layout
(form-to [:post "/user/add"]
(user-fields user)
(submit-button "Add user"))))
(defpage [:post "/user/add"] {:as user}
(if (valid? user)
(layout
[:p "User added!"])
(render "/user/add" user)))
(defpartial layout [& content]
(html5
[:head
[:title "Forms"]]
[:body
content]))
(defpartial user-fields [{:keys [firstname lastname]}]
(label "firstname" "First name: ")
(text-field "firstname" firstname)
(label "lastname" "Last name: ")
(text-field "lastname" lastname))
(defpage "/user/add" {:as user}
(layout
(form-to [:post "/user/add"]
(user-fields user)
(submit-button "Add user"))))
(require '[noir.response :as resp])
(defn valid? [{:keys [firstname lastname]}]
true)
(defpage [:post "/user/add"] {:as user}
(if (valid? user)
(layout
[:p "User added!"])
(render "/user/add" user)))
(require '[noir.validation :as vali])
(defn valid? [{:keys [firstname lastname]}]
(vali/rule (vali/min-length? firstname 5)
[:firstname "Your first name must have more than 5 letters."])
(vali/rule (vali/has-value? lastname)
[:lastname "You must have a last name"])
(not (vali/errors? :lastname :firstname)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.