Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (69 sloc) 2.28 KB
(ns signup
"Minimal Signup form"
(:use compojure.core, ring.adapter.jetty)
(:require [compojure.route :as route])
(:use hiccup.core)
(:use [ :only (include-css)])
(:use form-dot-clj.core)
(:use form-dot-clj.jquery-tools))
;; In slime ^c^k to compile this file
;; To start a webserver running this example app
;; (signup/start)
;; To stop the webserver
;; (signup/stop)
(def-field username
[:maxlength 20]
[:pattern "[a-zA-Z]+" "Only alphanumeric characters please"]
[:no-match #"(?i)(root|admin)" "Sorry that username is reserved"])
(def-field first-name [:maxlength 50])
(def-field last-name [:maxlength 50])
(def-field email [:email "Sorry, we cannot handle that email address"])
(def-field password [:maxlength 50])
(defn confirm-password
"A user defined validation function."
[params errors]
(if-not (= (params "confirm-password")
(params "password"))
{:password "Password and confirmation are not the same."}
(def-form signup
{:size 20 :required "Required" :check-fns [confirm-password]}
:username (textbox username)
:first-name (textbox first-name)
:last-name (textbox last-name {:required false})
:email (textbox email)
:password (textbox password {:type "password"})
:confirm-password (textbox password {:type "password"}))
(defn show-form [params errors]
[:title "Minimal Sign-up Form"]
(include-js signup "myform")]
[:form#myform {:action "/" :method "post"}
(show-controls signup params errors)
(default-submit "Sign Up")]]]))
(defn success [params]
[:h1 "Successful Post"]
(str params)))
(defroutes routing
(GET "/" [] (show-form {} {}))
(POST "/" {params :params}
(on-post signup params success show-form))
(html [:h1 "Page not found"])))
(def server (atom nil))
(defn stop []
(if-not (nil? @server)
(.stop @server)
(reset! server nil))))
(defn start []
(let [s (run-jetty routing {:port 8081 :join? false})]
(reset! server s)))