Skip to content

Instantly share code, notes, and snippets.

Last active Dec 14, 2015
What would you like to do?
(ns helloworld.handler
(:use compojure.core)
(:require [compojure.handler :as handler]
[compojure.route :as route]
[hiccup.form :as form]
[hiccup.core :refer [html]]
[noir.validation :as vali]))
;; レイアウト
(defn layout [& content]
[:head [:title "Form validation using lib-noir."]]
;; エラーメッセージ
(defn error-message [[first-error]]
[:span.error first-error])
;; 日付・時刻をPOSTするフォーム
(defn date-time-form [& [{:keys [date time]}]]
[:post "."]
(form/label "date" "日付")
(form/text-field "date" date)
;; バリデーション失敗時はエラーメッセージ
(vali/on-error :date error-message)]
(form/label "time" "時刻")
(form/text-field "time" time)
;; バリデーション失敗時はエラーメッセージ
(vali/on-error :time error-message)]
[:div (form/submit-button "送信")])))
(def regex-date-pattern
(re-pattern "^\\d{4}/(1[0-2]|0?[1-9])/(0?[1-9]|[1-2][0-9]|3[01])$"))
(def regex-time-pattern
(re-pattern "^([01]?[0-9]|2[0-3]):([0-5][0-9])$"))
(defn valid-date-time? [{:keys [date time]}]
;; dateをバリデート
(vali/rule (re-matches regex-date-pattern date)
;; 失敗したら :date にエラーメッセージ追加
[:date "日付を入力してください(yyyy/MM/dd)"])
;; timeをバリデート
(vali/rule (re-matches regex-time-pattern time)
;; 失敗したら :time にエラーメッセージ追加
[:time "時刻を入力してください(HH:mm)"])
;; バリデーション結果を返す
(not (vali/errors? :date :time)))
;; POSTされた値を表示
(defn show-date-time [{:keys [date time]}]
[:p (str "日付は「" date "」です。")]
[:p (str "時刻は「" time "」です。")]))
(defroutes app-routes
(GET "/" [] (date-time-form))
(POST "/" {param :params}
(if (valid-date-time? param)
;; バリデーション成功
(show-date-time param)
;; バリデーション失敗
(date-time-form param)))
(route/not-found "Not Found"))
(def app
(-> app-routes
(defproject helloworld "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url ""
:dependencies [[org.clojure/clojure "1.4.0"]
[compojure "1.1.5"]
[lib-noir "0.4.6"]
[hiccup "1.0.2"]]
:plugins [[lein-ring "0.8.2"]]
:ring {:handler helloworld.handler/app}
{:dev {:dependencies [[ring-mock "0.1.3"]]}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment