Skip to content

Instantly share code, notes, and snippets.

@jjl

jjl/README.md Secret

Created June 26, 2017 11:47
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 jjl/67658ccfff52a9cfb5b82f124bb2c82e to your computer and use it in GitHub Desktop.
Save jjl/67658ccfff52a9cfb5b82f124bb2c82e to your computer and use it in GitHub Desktop.

The Irresponsible Clojure Guild presents...

Formula

webforms, meet spec

Usage

(ns my.forms
 (:require [clojure.spec.alpha :as s]
           [irresponsible.formula :as f]
		   [irresponsible.formula.conformers :as c]))

;; declare our fields and their conformers
(s/def ::username (f/field "username" :conform (c/min-len 8)))
(s/def ::password (f/field "password" :conform (c/min-len 8)))
(s/def ::xsrf
(s/def ::login-form (f/form :req [::username ::password]))

A note about Conformers

Though you are used to defining specs by predicates, internally, the conformer function is the basic unit of operation. Where a predicate would return false or nil in the case of failure, a conformer returns :clojure.spec.alpha/invalid

Example:

(s/def ::foo string?)
;; internally, this expands to something like this:
(defn string-conformer [s]
  (if (string? s)
    s
    ::s/invalid))

Because form values are received as strings and typically validated in the attempt to parse them, this library uses conformers instead of predicates, like spec does internally and like it may yet end up doing in the public API.

Getting Started

For this example, we assume you are using ring with wrap-params, but ring is not required, we just take a map

(ns my-app.routes
 (:require [clojure.spec.alpha :as s]
           [irresponsible.formula :as f]
           [irresponsible.formula.conformers :as c]))

;;;;;;;; field declarations

;;; the ::username field reads the parameter "username" and checks it is 8 chars+
(s/def ::username
 (f/field "username"
   :conform (c/min-len 8)
   :error "Must be at least 8 characters long"))

;;; the ::password field reads the parameter "password" and checks it is 8 chars+
(s/def ::password
 (f/field "password"
   :conform (c/min-len 8)
   :error "Must be at least 8 characters long"))

;;;;;;;;; form declaration

(s/def ::login-form (f/form :req [::username ::password]))


;;;;;;;;; usage is just like spec!

(defn read-login-form
  "This is a quick helper to demonstrate conforming a form.
   Its main purpose is to return nil when the form data is invalid
   returns: map of field names to values"
  [params]
  (let [r (s/conform ::login-form params)]
    (when-not (f/invalid? r) ;; invalid? just checks if it's ::s/invalid
      r)))

(defn login-post
  "This is a standard ring handler function called in response to a POST request"
  [{:keys [params]}]
  (if-let [r (read-login-form params)]
    (let [errors (f/errors (s/explain-data ::login-form params))]
      (render-template "template.html" {:params params :errors errors}))
    (if-let [u (lookup-user {:username (::username form) :password (::password form)})]
      (do-login u)
      (render-template "template.html" {:params params :errors {:page "Your username and/or password were wrong. Please try again"}}))))
  

And here's a html-based template for the form. It uses thymeleaf, but you should be able to readily translate it to whatever you're using:

<!doctype html>
<html>
<head>
</head>
<body>
  <form action="/login" method="post">
    <div class="field">
      <label for="username">Username:</label>
      <input type="text" th:value="${params.username}" id="username">
      <span class="error" th:if="${errors.username}" th:text="${errors.username}"/>error message will be written here</span>
    </div>
    <div class="field">
      <label for="password">Password:</label>
      <input type="password" id="password">
      <span class="error" th:if="${errors.password}" th:text="${errors.password}"/>error message will be written here</span>
    </div>
    <div class="field">
      <button type="submit" id="submit">Log in!</button>
    </div>
  </form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment