For putting Validation checks using Clojure code, clojure noir library checks are used.

One needs to first create a form page using basic html for placing the checks.As an Example here, I am using a registration page text boxes to put the validation checks over the required fields.

<label for="id">
{% if id-error %}
{% endif %}
<input id="id" name="id" placeholder="User Name" type="text" value={{id}}></input>
<label for="email">
{% if id-error %}
{% endif %}
<input id="email" name="email" placeholder="Email" type="text" value={{email}}></input>
<label for="pass">
{% if pass-error %}
{% endif %}
<input id="pass" name="pass" placeholder="Password" type="password" value={{pass}}></input>
<label for="confirmPass">
{% if confirmPass-error %}
{% endif %}
<input id="confirmPass" name="confirmPass" placeholder="Confirm Password" type="password" value={{confirmPass}}></input>

Then we move ahead by creating a register.clj file where all validations are defined under the namespace definition:

(ns projectName.routes.register
  (:use compojure.core)
            [noir.session :as session]
            [noir.response :as resp]
            [noir.validation :as vali])

Next is to create the route that defines the register function which is called when the register page is rendered.

(defroutes register-routes
  (GET "/register" []

the above route calls the following function:

(defn register [& [id]]
    {:id id
     :id-error (vali/on-error :id first)
     :email-error (vali/on-error :email first)
     :pass-error (vali/on-error :pass first)
     :confirmPass-error (vali/on-error :confirmPass first)}))

where, [id] for any number of arguments passed, (layout/render) renders with that html page where validations are required to be placed, :id-error/email-error are variables placed in the html form that will define the error block, (vali/on-error) denotes noir validation structure.

Next step is to define the errors and put checks over the required fields i.e; defining the errors for above created check blocks.

(defn valid? [id email pass confirmPass]
  (vali/rule (vali/has-value? id)
             [:id "Username is required"])
  (vali/rule (vali/is-email? email)
             [:email "Invalid Email Address"])
  (vali/rule (vali/min-length? id 8)                                      
             [:id "Username must be at least 8 characters long"])      
  (vali/rule (vali/min-length? pass 8)
             [:pass "Password must be at least 8 characters long"])
  (vali/rule (= pass confirmPass)
             [:confirmPass "Passwords does not match"])
  (not (vali/errors? :id :email :pass :confirmPass)))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s