Created
July 24, 2011 21:45
-
-
Save ibdknox/1103146 to your computer and use it in GitHub Desktop.
forms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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