Skip to content

Instantly share code, notes, and snippets.

@cgmartin
Created July 13, 2013 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgmartin/5990806 to your computer and use it in GitHub Desktop.
Save cgmartin/5990806 to your computer and use it in GitHub Desktop.
noir validation example
(ns my.routes.auth
(:use compojure.core
[hiccup.util :only (url to-str)])
(:require [ring.util.response :as response]
[taoensso.tower :as t]
[noir.session :as session]
[noir.cookies :as cookies]
[noir.validation :as vali]
[noir.util.crypt :as crypt]))
(defn registration-valid? [name email password password2]
(vali/rule (vali/has-value? name)
[:name "Name is required"])
(vali/rule (vali/has-value? email)
[:email "Email is required"])
(vali/rule (nil? (db/get-user-by-email email))
[:email "Email already registered"])
(vali/rule (email? email)
[:email "Invalid email address"])
(vali/rule (vali/min-length? password 5)
[:password "Password must be at least 5 characters"])
(vali/rule (= password password2)
[:password2 "Entered passwords do not match"])
(not (vali/errors? :email :name :password :password2)))
(defn register-page [& [name email]]
(render "register.html"
{:register-form {:email {:value email
:error (vali/on-error :email first)}
:name {:value name
:error (vali/on-error :name first)}
:password {:error (vali/on-error :password first)}
:password2 {:error (vali/on-error :password2 first)}
:passswords {:error (vali/errors? :password :password2)}
:autofocus "name"}}))
(defn process-registration [name email password password2 next-route]
(if (registration-valid? name email password password2)
(try
(let [user-id (db/user-register name email password)]
(session/put! :user-id user-id)
(response/redirect (to-str (url next-route))))
(catch Exception ex
(vali/rule false [:email (.getMessage ex)]) ; FIXME fix error message
(register-page name email)))
(register-page name email)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment